1. 程式人生 > 實用技巧 >18.增量式爬蟲

18.增量式爬蟲

18.增量式爬蟲

增量式爬蟲

引言:

當我們在瀏覽相關網頁的時候會發現,某些網站定時會在原有網頁資料的基礎上更新一批資料,例如某電影網站會實時更新一批最近熱門的電影。小說網站會根據作者創作的進度實時更新最新的章節資料等等。那麼,類似的情景,當我們在爬蟲的過程中遇到時,我們是不是需要定時更新程式以便能爬取到網站中最近更新的資料呢?

一.增量式爬蟲

  • 概念:通過爬蟲程式監測某網站資料更新的情況,以便可以爬取到該網站更新出的新資料。
  • 如何進行增量式的爬取工作:
    • 在傳送請求之前判斷這個URL是不是之前爬取過
    • 在解析內容後判斷這部分內容是不是之前爬取過
    • 寫入儲存介質時判斷內容是不是已經在介質中存在
      • 分析:

        不難發現,其實增量爬取的核心是去重

        , 至於去重的操作在哪個步驟起作用,只能說各有利弊。在我看來,前兩種思路需要根據實際情況取一個(也可能都用)。第一種思路適合不斷有新頁面出現的網站,比如說小說的新章節,每天的最新新聞等等;第二種思路則適合頁面內容會更新的網站。第三個思路是相當於是最後的一道防線。這樣做可以最大程度上達到去重的目的。

  • 去重方法
    • 將爬取過程中產生的url進行儲存,儲存在redis的set中。當下次進行資料爬取時,首先對即將要發起的請求對應的url在儲存的url的set中做判斷,如果存在則不進行請求,否則才進行請求。
    • 對爬取到的網頁內容進行唯一標識的制定,然後將該唯一表示儲存至redis的set中。當下次爬取到網頁資料的時候,在進行持久化儲存之前,首先可以先判斷該資料的唯一標識在redis的set中是否存在,在決定是否進行持久化儲存。

二.專案案例

- 需求:爬取4567tv網站中所有的電影詳情資料。

爬蟲檔案:

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from redis import Redis
from incrementPro.items import IncrementproItem
class MovieSpider(CrawlSpider):
    name = 'movie'
    # allowed_domains = ['www.xxx.com']
start_urls = ['http://www.4567tv.tv/frim/index7-11.html'] rules = ( Rule(LinkExtractor(allow=r'/frim/index7-\d+\.html'), callback='parse_item', follow=True), ) #建立redis連結物件 conn = Redis(host='127.0.0.1',port=6379) def parse_item(self, response): li_list = response.xpath('//li[@class="p1 m1"]') for li in li_list: #獲取詳情頁的url detail_url = 'http://www.4567tv.tv'+li.xpath('./a/@href').extract_first() #將詳情頁的url存入redis的set中 ex = self.conn.sadd('urls',detail_url) if ex == 1: print('該url沒有被爬取過,可以進行資料的爬取') yield scrapy.Request(url=detail_url,callback=self.parst_detail) else: print('資料還沒有更新,暫無新資料可爬取!') #解析詳情頁中的電影名稱和型別,進行持久化儲存 def parst_detail(self,response): item = IncrementproItem() item['name'] = response.xpath('//dt[@class="name"]/text()').extract_first() item['kind'] = response.xpath('//div[@class="ct-c"]/dl/dt[4]//text()').extract() item['kind'] = ''.join(item['kind']) yield item

管道檔案:

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

from redis import Redis
class IncrementproPipeline(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 = {
            'name':item['name'],
            'kind':item['kind']
        }
        print(dic)
        self.conn.lpush('movieData',dic)
        return item

- 需求:爬取糗事百科中的段子和作者資料。

爬蟲檔案:

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from incrementByDataPro.items import IncrementbydataproItem
from redis import Redis
import hashlib
class 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),
        Rule(LinkExtractor(allow=r'/text/$'), callback='parse_item', follow=True),
    )
    #建立redis連結物件
    conn = Redis(host='127.0.0.1',port=6379)
    def parse_item(self, response):
        div_list = response.xpath('//div[@id="content-left"]/div')

        for div in div_list:
            item = IncrementbydataproItem()
            item['author'] = div.xpath('./div[1]/a[2]/h2/text() | ./div[1]/span[2]/h2/text()').extract_first()
            item['content'] = div.xpath('.//div[@class="content"]/span/text()').extract_first()

            #將解析到的資料值生成一個唯一的標識進行redis儲存
            source = item['author']+item['content']
            source_id = hashlib.sha256(source.encode()).hexdigest()
            #將解析內容的唯一表示儲存到redis的data_id中
            ex = self.conn.sadd('data_id',source_id)

            if ex == 1:
                print('該條資料沒有爬取過,可以爬取......')
                yield item
            else:
                print('該條資料已經爬取過了,不需要再次爬取了!!!')



管道檔案:

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

from redis import Redis
class IncrementbydataproPipeline(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']
        }
        # print(dic)
        self.conn.lpush('qiubaiData', dic)
        return item