scrapy-splash抓取动态数据例子五
Posted shaomine
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scrapy-splash抓取动态数据例子五相关的知识,希望对你有一定的参考价值。
一、介绍
本例子用scrapy-splash抓取智能电视网网站给定关键字抓取咨询信息。
给定关键字:打通;融合;电视
抓取信息内如下:
1、资讯标题
2、资讯链接
3、资讯时间
4、资讯来源
二、网站信息
三、数据抓取
针对上面的网站信息,来进行抓取
1、首先抓取信息列表
抓取代码:sels = site.xpath(\'//div[@class="listl list2"]/ul/li\')
2、抓取标题
首先列表页面,根据标题和日期来判断是否自己需要的资讯,如果是,就今日到资讯对应的链接,来抓取来源,如果不是,就不用抓取了
抓取代码:titles = sel.xpath(\'.//h3/a/text()\')
3、抓取链接
抓取代码:url = \'http://news.znds.com\' + str(sel.xpath(\'.//h3/a/@href\')[0].extract())
4、抓取日期
抓取代码:strdate = str(sel.xpath(\'.//span/text()\').extract())
5、抓取来源
抓取代码:sources = site.xpath(\'//span[@class="spanimg2"]/text()\')
四、完整代码
# -*- coding: utf-8 -*- import scrapy from scrapy import Request from scrapy.spiders import Spider from scrapy_splash import SplashRequest from scrapy_splash import SplashMiddleware from scrapy.http import Request, htmlResponse from scrapy.selector import Selector from scrapy_splash import SplashRequest from splash_test.items import SplashTestItem import IniFile import sys import os import re import time reload(sys) sys.setdefaultencoding(\'utf-8\') # sys.stdout = open(\'output.txt\', \'w\') class zndsSpider(Spider): name = \'znds\' configfile = os.path.join(os.getcwd(), \'splash_test\\spiders\\setting.conf\') cf = IniFile.ConfigFile(configfile) information_wordlist = cf.GetValue("section", "information_keywords").split(\';\') websearch_urls = cf.GetValue("znds", "websearchurl").split(\';\') start_urls = [] for word in websearch_urls: start_urls.append(word) # request需要封装成SplashRequest def start_requests(self): for url in self.start_urls: yield SplashRequest(url , self.parse , args={\'wait\': \'2\'} ) def Comapre_to_days(self,leftdate, rightdate): \'\'\' 比较连个字符串日期,左边日期大于右边日期多少天 :param leftdate: 格式:2017-04-15 :param rightdate: 格式:2017-04-15 :return: 天数 \'\'\' l_time = time.mktime(time.strptime(leftdate, \'%Y-%m-%d\')) r_time = time.mktime(time.strptime(rightdate, \'%Y-%m-%d\')) result = int(l_time - r_time) / 86400 return result def date_isValid(self, strDateText): \'\'\' 判断日期时间字符串是否合法:如果给定时间大于当前时间是合法,或者说当前时间给定的范围内 :param strDateText: 四种格式 \'2小时前\'; \'2天前\' ; \'昨天\' ;\'2017.2.12 \' :return: True:合法;False:不合法 \'\'\' currentDate = time.strftime(\'%Y-%m-%d\') datePattern = re.compile(r\'\\d{4}-\\d{1,2}-\\d{1,2}\') strDate = re.findall(datePattern, strDateText) if len(strDate) == 1: if self.Comapre_to_days(currentDate,strDate[0])==0: return True,currentDate return False, \'\' def parse(self, response): site = Selector(response) # keyword = response.meta[\'keyword\'] sels = site.xpath(\'//div[@class="listl list2"]/ul/li\') for sel in sels: titles = sel.xpath(\'.//h3/a/text()\') if len(titles)>0: title = str(titles[0].extract()) strdate = str(sel.xpath(\'.//span/text()\').extract()) flag,date =self.date_isValid(strdate) if flag: url = \'http://news.znds.com\' + str(sel.xpath(\'.//h3/a/@href\')[0].extract()) for keyword in self.information_wordlist: if title.find(keyword)>-1:yield SplashRequest(url , self.parse_item , args={\'wait\': \'1\'}, meta={\'date\': date, \'url\': url, \'keyword\': keyword,\'title\':title} ) def parse_item(self, response): site = Selector(response) it = SplashTestItem() it[\'title\'] = response.meta[\'title\'] it[\'url\'] = response.meta[\'url\'] it[\'date\'] = response.meta[\'date\'] it[\'keyword\'] = response.meta[\'keyword\'] sources = site.xpath(\'//span[@class="spanimg2"]/text()\') if len(sources)>0: source =str(sources[0].extract()).replace(u\'来源: \',\'\') it[\'source\'] = source return it
以上是关于scrapy-splash抓取动态数据例子五的主要内容,如果未能解决你的问题,请参考以下文章
使用 scrapy-splash 会显着影响抓取速度吗? [关闭]