增量式爬虫
概念:通过爬虫检测某网站的更新情况,以便可以爬取最新的数据。
如何进行增量式的爬虫工作?
- 在发送请求之前判断这个url是不是被之前爬取过
- 在解析内容后判断这部分内容是不是之前爬取过
- 在写入存储介质的时候,判断内容是不是已经在介质中存在
分析:
不难发现,其实增量爬取的核心是去重, 至于去重的操作在哪个步骤起作用,只能说各有利弊。在我看来,前两种思路需要根据实际情况取一个(也可能都用)。第一种思路适合不断有新页面出现的网站,比如说小说的新章节,每天的最新新闻等等;第二种思路则适合页面内容会更新的网站。第三个思路是相当于是最后的一道防线。这样做可以最大程度上达到去重的目的。
去重的方法:
- 将爬取过程中产生的url进行存储,存储在redis的set中。当下次进行数据爬取时,首先对即将要发起的请求对应的url在存储的url的set中做判断,如果存在则不进行请求,否则才进行请求。
- 对爬取到的网页内容进行唯一标识的制定,然后将该唯一表示存储至redis的set中。当下次爬取到网页数据的时候,在进行持久化存储之前,首先可以先判断该数据的唯一标识在redis的set中是否存在,在决定是否进行持久化存储。
- 需求:爬取4567tv网站中所有的电影详情数据。
在发送请求之前判断这个url是不是被之前爬取过
爬虫文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # -*- 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 = 'https://www.4567tv.tv' +response.xpath( '//li[@class="col-md-6 col-sm-4 col-xs-3"]/div/a/@href' ).extract() for url in detail_url_list: #ex == 1:set中没有存储url ex = conn.sadd( 'movies_url' ,url) 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 |
items.py
1 2 3 4 5 6 7 | import scrapy class Increment1ProItem(scrapy.Item): # define the fields for your item here like: name = scrapy.Field() actor = scrapy.Field() |
pipelines.py
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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): # dic = { # 'name':item['name'], # 'axtor':item['actor'] # } print( '有新数据被爬取到,正在入库......' ) self.conn.lpush( 'movie_data' ,item) return item |
settings.py
1 2 3 4 5 | USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36' ROBOTSTXT_OBEY = False ITEM_PIPELINES = { 'increment1_Pro.pipelines.Increment1ProPipeline' : 300, } |
在解析内容后判断这部分内容是不是之前爬取过
- 需求:爬取糗事百科中段子的标题和作者
爬虫文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # -*- coding: utf-8 -*- import scrapy from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule from increment2_Pro.items import Increment2ProItem 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), ) 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' ] #自己制定了一种形式的数据指纹 hashValue = hashlib.sha256(source.encode()).hexdigest() ex = conn.sadd( 'qiubai_hash' ,hashValue) if ex == 1: yield item else : print( '没有更新数据可爬!!!' ) |
pipelines.py
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律