日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python增量爬虫_python爬虫Scrapy框架之增量式爬虫

發布時間:2025/3/15 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python增量爬虫_python爬虫Scrapy框架之增量式爬虫 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一 增量式爬蟲

什么時候使用增量式爬蟲:

增量式爬蟲:需求 當我們瀏覽一些網站會發現,某些網站定時的會在原有的基礎上更新一些新的數據。如一些電影網站會實時更新最近熱門的電影。那么,當我們在爬蟲的過程中遇到這些情況時,我們是不是應該定期的更新程序以爬取到更新的新數據?那么,增量式爬蟲就可以幫助我們來實現

二 增量式爬蟲

概念

通過爬蟲程序檢測某網站數據更新的情況,這樣就能爬取到該網站更新出來的數據

如何進行增量式爬取工作:

在發送請求之前判斷這個URL之前是不是爬取過

在解析內容之后判斷該內容之前是否爬取過

在寫入存儲介質時判斷內容是不是在該介質中

增量式的核心是 去重

去重的方法

將爬取過程中產生的URL進行存儲,存入到redis中的set中,當下次再爬取的時候,對在存儲的URL中的set中進行判斷,如果URL存在則不發起請求,否則 就發起請求

對爬取到的網站內容進行唯一的標識,然后將該唯一標識存儲到redis的set中,當下次再爬取數據的時候,在進行持久化存儲之前,要判斷該數據的唯一標識在不在redis中的set中,如果在,則不在進行存儲,否則就存儲該內容

三 實列

爬取4567tv網站中所有的電影詳情數據

movie.py

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

import scrapy

from scrapy.linkextractors import LinkExtractor

from scrapy.spiders import CrawlSpider, Rule

from redis import Redis

from increment1_Pro.items import Increment1ProItem

class MovieSpider(CrawlSpider):

name = 'movie'

# allowed_domains = ['www.xxx.com']

start_urls = ['https://www.4567tv.tv/index.php/vod/show/id/7.html']

rules = (

Rule(LinkExtractor(allow=r'/index.php/vod/show/id/7/page/\d+\.html'), callback='parse_item', follow=True),

)

def parse_item(self, response):

conn = Redis(host='127.0.0.1',port=6379)

detail_url_list= response.xpath('//li[@class="col-md-6 col-sm-4 col-xs-3"]/div/a/@href').extract()

for url in detail_url_list:

ex = conn.sadd('movies_url',url)

#等于1 的時候 說明數據還沒有存儲到redis中 等于0 的時候 說明redis中已經存在該數據

if ex == 1:

yield scrapy.Request(url=url,callback=self.parse_detail)

else:

print("網站中無數據更新,沒有可爬取得數據!!!")

def parse_detail(self,response):

item = Increment1ProItem()

item['name']=response.xpath('/html/body/div[1]/div/div/div/div[2]/h1/text()').extract_first()

item['actor']=response.xpath('/html/body/div[1]/div/div/div/div[2]/p[3]/a/text()').extract_first()

yield item

# 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 Redis

class Increment1ProPipeline(object):

conn = None

def open_spider(self,spider):

self.conn = Redis(host='127.0.0.1',port=6379)

def process_item(self, item, spider):

print('有新的數據正在入庫')

self.conn.lpush('movie_data',item)

return item

爬取糗事百科中的內容和作者

qiubai.py

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

import scrapy

from scrapy.linkextractors import LinkExtractor

from scrapy.spiders import CrawlSpider, Rule

from redis import Redis

from increment2_Pro.items import Increment2ProItem

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),

)

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()

source = item['author'] + item['content']

sourse = item['content']+item['author']

#自己定制一種形式得數據指紋

hashvalue = hashlib.sha256(sourse.encode()).hexdigest()

ex = conn.sadd('qiubai_hash',hashvalue)

if ex == 1:

yield item

else:

print('沒有可更新的數據可爬取')

# 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 Redis

class 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('爬取到一條數據,正在入庫......')

return item

總結

以上是生活随笔為你收集整理的python增量爬虫_python爬虫Scrapy框架之增量式爬虫的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。