scrapy-splash抓取动态数据例子六
一、介紹
本例子用scrapy-splash抓取中廣互聯(lián)網(wǎng)站給定關(guān)鍵字抓取咨詢信息。
給定關(guān)鍵字:打通;融合;電視
抓取信息內(nèi)如下:
1、資訊標(biāo)題
2、資訊鏈接
3、資訊時(shí)間
4、資訊來源
?
二、網(wǎng)站信息
?
?
?
?
?
?
三、數(shù)據(jù)抓取
針對(duì)上面的網(wǎng)站信息,來進(jìn)行抓取
1、首先抓取信息列表
抓取代碼:sels = site.xpath('//li[@class="content_list clearfix"]')
2、抓取標(biāo)題
首先列表頁面,根據(jù)標(biāo)題和日期來判斷是否自己需要的資訊,如果是,就今日到資訊對(duì)應(yīng)的鏈接,來抓取來源,如果不是,就不用抓取了
抓取代碼:titles = sel.xpath('.//div[@class="content_listtext"]/a/h3/text()')
3、抓取鏈接
抓取代碼:url = 'http://www.tvoao.com' + str(sel.xpath('.//a/@href')[0].extract())
4、抓取日期
抓取代碼:strdates = sel.xpath('.//div[@class="listtext_label"]/span/text()')
5、抓取來源
抓取代碼:sources = site.xpath('//div[@class="headlines_source"]/span[2]/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 timereload(sys) sys.setdefaultencoding('utf-8')sys.stdout = open('output.txt', 'w')class tvoaoSpider(Spider):name = 'tvoao'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("tvoao", "websearchurl").split(';')start_urls = []for url in websearch_urls:start_urls.append(url)# request需要封裝成SplashRequestdef start_requests(self):for url in self.start_urls:yield SplashRequest(url, self.parse, args={'wait': '2'})def Comapre_to_days(self,leftdate, rightdate):'''比較連個(gè)字符串日期,左邊日期大于右邊日期多少天:param leftdate: 格式:2017-04-15:param rightdate: 格式:2017-04-15:return: 天數(shù)'''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) / 86400return resultdef date_isValid(self, strDateText):'''判斷日期時(shí)間字符串是否合法:如果給定時(shí)間大于當(dāng)前時(shí)間是合法,或者說當(dāng)前時(shí)間給定的范圍內(nèi):param strDateText: 四種格式 '2小時(shí)前'; '2天前' ; '昨天' ;'2017.2.12 ':return: True:合法;False:不合法'''currentDate = time.strftime('%Y-%m-%d')datePattern = re.compile(r'\d{4}-\d{2}-\d{2}')strDate = re.findall(datePattern, strDateText)if len(strDate) == 1:if self.Comapre_to_days(currentDate, strDate[0]) == 0:return True, currentDatereturn False, ''def parse(self, response):site = Selector(response)sels = site.xpath('//li[@class="content_list clearfix"]')for sel in sels:strdates = sel.xpath('.//div[@class="listtext_label"]/span/text()')if len(strdates)>0:flag, date = self.date_isValid(str(strdates[0].extract()))if flag:titles = sel.xpath('.//div[@class="content_listtext"]/a/h3/text()')if len(titles) > 0:title = str(titles[0].extract())url = 'http://www.tvoao.com' + str(sel.xpath('.//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('//div[@class="headlines_source"]/span[2]/text()')if len(sources)>0:it['source'] = str(sources[0].extract()).replace(u'來源: ', '')return it
?
轉(zhuǎn)載于:https://www.cnblogs.com/shaosks/p/6970712.html
總結(jié)
以上是生活随笔為你收集整理的scrapy-splash抓取动态数据例子六的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 环形队列的实现
- 下一篇: iOS 版 Skype支持群组语音聊天