本例子用scrapy-splash爬取超級TV網站的資訊信息,輸入給定關鍵字抓取微信資訊信息。web
給定關鍵字:數字;融合;電視微信
抓取信息內以下:app
一、資訊標題scrapy
二、資訊連接ide
三、資訊時間網站
四、資訊來源url
針對上面的網站信息,來進行抓取spa
一、首先抓取信息列表3d
抓取代碼:sels = site.xpath('//ul[@class="la_list"]/li')code
二、抓取標題
抓取代碼:title = str(sel.xpath('.//h4/a/text()')[0].extract())
三、抓取連接
抓取代碼:url = str(sel.xpath('.//h4/a/@href')[0].extract())
四、抓取日期
抓取代碼:strdates = sel.xpath('.//div[@class="time"]/span/text()')
五、抓取來源
抓取代碼:sources = site.xpath('//span[@class="wzof"]/a/text()');或sources = site.xpath('//span[@class="wzof"]/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 scrapy_ott.items import SplashTestItem from scrapy_ott.mongoDB import mongoDbBase import scrapy_ott.IniFile import sys import os import re import time reload(sys) sys.setdefaultencoding('utf-8') # sys.stdout = open('output.txt', 'w') class chaojitvSpider(Spider): name = 'chaojitv' db = mongoDbBase() configfile = os.path.join(os.getcwd(), 'scrapy_ott\setting.conf') cf = scrapy_ott.IniFile.ConfigFile(configfile) information_wordlist = cf.GetValue("section", "information_keywords").split(';') websearchurl_list = cf.GetValue("chaojitv", "websearchurl").split(';') start_urls = [] for url in websearchurl_list: start_urls.append(url) # 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{2}-\d{2}') strDate = re.findall(datePattern, strDateText) if len(strDate) == 1: if self.Comapre_to_days(currentDate, strDate[0]) <= 1: return True, strDate[0] return False, '' def parse(self, response): site = Selector(response) sels = site.xpath('//ul[@class="la_list"]/li') for sel in sels: strdates = sel.xpath('.//div[@class="time"]/span/text()') if len(strdates)>0: flag, date = self.date_isValid(str(strdates[0].extract())) if flag: title = str(sel.xpath('.//h4/a/text()')[0].extract()) for keyword in self.information_wordlist: if title.find(keyword) > -1: url = str(sel.xpath('.//h4/a/@href')[0].extract()) 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="wzof"]/a/text()') if len(sources)>0: it['source'] = sources[0].extract() else: #時間:2017-06-15 來源:環球網 編輯:葉子 sources = site.xpath('//span[@class="wzof"]/text()') if len(sources)>0: ss= str(sources[0].extract()).split(':') if len(ss)>2: it['source']= ss[2].replace(u'編輯: ','').replace(' ','') self.db.SaveInformation(it) return it