python爬蟲(chóng)scrapy框架之增量式爬蟲(chóng)的示例代碼
scrapy框架之增量式爬蟲(chóng)
一 、增量式爬蟲(chóng)什么時(shí)候使用增量式爬蟲(chóng):增量式爬蟲(chóng):需求 當(dāng)我們?yōu)g覽一些網(wǎng)站會(huì)發(fā)現(xiàn),某些網(wǎng)站定時(shí)的會(huì)在原有的基礎(chǔ)上更新一些新的數(shù)據(jù)。如一些電影網(wǎng)站會(huì)實(shí)時(shí)更新最近熱門(mén)的電影。那么,當(dāng)我們?cè)谂老x(chóng)的過(guò)程中遇到這些情況時(shí),我們是不是應(yīng)該定期的更新程序以爬取到更新的新數(shù)據(jù)?那么,增量式爬蟲(chóng)就可以幫助我們來(lái)實(shí)現(xiàn)
二 、增量式爬蟲(chóng)概念:通過(guò)爬蟲(chóng)程序檢測(cè)某網(wǎng)站數(shù)據(jù)更新的情況,這樣就能爬取到該網(wǎng)站更新出來(lái)的數(shù)據(jù)
如何進(jìn)行增量式爬取工作:在發(fā)送請(qǐng)求之前判斷這個(gè)URL之前是不是爬取過(guò)在解析內(nèi)容之后判斷該內(nèi)容之前是否爬取過(guò)在寫(xiě)入存儲(chǔ)介質(zhì)時(shí)判斷內(nèi)容是不是在該介質(zhì)中
增量式的核心是 去重去重的方法:將爬取過(guò)程中產(chǎn)生的URL進(jìn)行存儲(chǔ),存入到redis中的set中,當(dāng)下次再爬取的時(shí)候,對(duì)在存儲(chǔ)的URL中的set中進(jìn)行判斷,如果URL存在則不發(fā)起請(qǐng)求,否則 就發(fā)起請(qǐng)求對(duì)爬取到的網(wǎng)站內(nèi)容進(jìn)行唯一的標(biāo)識(shí),然后將該唯一標(biāo)識(shí)存儲(chǔ)到redis的set中,當(dāng)下次再爬取數(shù)據(jù)的時(shí)候,在進(jìn)行持久化存儲(chǔ)之前,要判斷該數(shù)據(jù)的唯一標(biāo)識(shí)在不在redis中的set中,如果在,則不在進(jìn)行存儲(chǔ),否則就存儲(chǔ)該內(nèi)容
三、示例爬蟲(chóng)文件
# -*- coding: utf-8 -*-import scrapyfrom scrapy.linkextractors import LinkExtractorfrom scrapy.spiders import CrawlSpider, Rulefrom redis import Redisfrom increment2_Pro.items import Increment2ProItemimport hashlibclass QiubaiSpider(CrawlSpider): name = ’qiubai’ # allowed_domains = [’www.xxx.com’] start_urls = [’https://www.qiushibaike.com/text/’] rules = ( Rule(LinkExtractor(allow=r’/text/page/d+/’), callback=’parse_item’, follow=True), ) def parse_item(self, response): div_list = response.xpath(’//div[@class='article block untagged mb15 typs_hot']’) conn = Redis(host=’127.0.0.1’,port=6379) for div in div_list: item = Increment2ProItem() item[’content’] = div.xpath(’.//div[@class='content']/span//text()’).extract() item[’content’] = ’’.join(item[’content’]) item[’author’] = div.xpath(’./div/a[2]/h2/text() | ./div[1]/span[2]/h2/text()’).extract_first() # 將當(dāng)前爬取的數(shù)據(jù)做哈希唯一標(biāo)識(shí)(數(shù)據(jù)指紋) sourse = item[’content’]+item[’author’] hashvalue = hashlib.sha256(sourse.encode()).hexdigest() ex = conn.sadd(’qiubai_hash’,hashvalue) if ex == 1:yield item else:print(’沒(méi)有可更新的數(shù)據(jù)可爬取’) # item = {} #item[’domain_id’] = response.xpath(’//input[@id='sid']/@value’).get() #item[’name’] = response.xpath(’//div[@id='name']’).get() #item[’description’] = response.xpath(’//div[@id='description']’).get() # return item
管道文件(管道文件也可以不用加)
from redis import Redisclass Increment2ProPipeline(object): conn = None def open_spider(self,spider): self.conn = Redis(host=’127.0.0.1’,port=6379) def process_item(self, item, spider): dic = { ’author’:item[’author’], ’content’:item[’content’] } self.conn.lpush(’qiubaiData’,dic) print(’爬取到一條數(shù)據(jù),正在入庫(kù)......’) return item
到此這篇關(guān)于python爬蟲(chóng)之scrapy框架之增量式爬蟲(chóng)的示例代碼的文章就介紹到這了,更多相關(guān)scrapy增量式爬蟲(chóng)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. web下載文件和跳轉(zhuǎn)的方法2. ASP中常用的22個(gè)FSO文件操作函數(shù)整理3. PHP函數(shù)原理理解詳談4. React+umi+typeScript創(chuàng)建項(xiàng)目的過(guò)程5. SharePoint Server 2019新特性介紹6. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析7. Vue+elementUI下拉框自定義顏色選擇器方式8. ASP的Global.asa文件技巧用法9. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法10. JSP頁(yè)面實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能
