笔记-scrapy-去重

1.      scrapy 去重

scrapy 版本:1.5.0

第一步是要找到去重的代码,scrapy在请求入列前去重,具体源码在scheduler.py:

def enqueue_request(self, request):

if not request.dont_filter and self.df.request_seen(request):

self.df.log(request, self.spider)

return False

dqok = self._dqpush(request)

if dqok:

self.stats.inc_value('scheduler/enqueued/disk', spider=self.spider)

else:

self._mqpush(request)

self.stats.inc_value('scheduler/enqueued/memory', spider=self.spider)

self.stats.inc_value('scheduler/enqueued', spider=self.spider)

return True

红色部分进行去重:

dont_filter在requests类中有提到,用于指定是否对请求进行去重

self.df.request_seen(request)是什么?

self.df = dupefilter,而dupefilter由dupefilter_cls = load_object(settings['DUPEFILTER_CLASS'])指定

文档settings中有提到DUPEFILTER_CLASS

Default: 'scrapy.dupefilters.RFPDupeFilter'

找到scrapy.dupefilters.RFPDupeFilter,其中有request_seen()

def request_seen(self, request):

fp = self.request_fingerprint(request)

if fp in self.fingerprints:

return True

self.fingerprints.add(fp)

if self.file:

self.file.write(fp + os.linesep)

简单来说是通过self.request_fingerprint(request)得到fp,判断是否存在于self.fingerprints中,如果存在,则返回True,否则返回None。

self.fingerprints=set() 它是一个指纹集合。

self.request_fingerprint()是什么?继续找

self.request_fingerprint()实际是调用自utils.request.py的request_fingerprint()

源码如下:

def request_fingerprint(request, include_headers=None):

"""

Return the request fingerprint.

"""

if include_headers:

include_headers = tuple(to_bytes(h.lower())

for h in sorted(include_headers))

cache = _fingerprint_cache.setdefault(request, {})

if include_headers not in cache:

fp = hashlib.sha1()

fp.update(to_bytes(request.method))

fp.update(to_bytes(canonicalize_url(request.url)))

fp.update(request.body or b'')

if include_headers:

for hdr in include_headers:

if hdr in request.headers:

fp.update(hdr)

for v in request.headers.getlist(hdr):

fp.update(v)

cache[include_headers] = fp.hexdigest()

return cache[include_headers]

有点杂,它返回的是request的伪hash码,它对request的method,处理后的url,body进行hash,返回hash值。

1.1.    总结

  1. 去重是在请求入队列之前进行的,dont_filter控制是否进行去重(默认为false去重);
  2. 通过request_seen()判断是否重复,实际过程是计算出request的指纹,去集合中匹配是否存在,;
  3. 使用request_fingerprint计算请求的指纹。

从代码中可以看到去重依赖的是set,这样的话在爬虫中断后会被清空,scrapy提供了一个方案:

将指纹保存到文件中,每次实例化时从文件中读取。

def __init__(self, path=None, debug=False):

self.file = None

self.fingerprints = set()

self.logdupes = True

self.debug = debug

self.logger = logging.getLogger(__name__)

if path:

self.file = open(os.path.join(path, 'requests.seen'), 'a+')

self.file.seek(0)

self.fingerprints.update(x.rstrip() for x in self.file)

笔记-scrapy-去重的更多相关文章

  1. 笔记-爬虫-去重/bloomfilter

    笔记-爬虫-去重/bloomfilter 1.      去重 为什么要去重? 页面重复:爬的多了,总会有重复的页面,对已爬过的页面肯定不愿意再爬一次. 页面更新:很多页面是会更新的,爬取这种页面时就 ...

  2. 笔记-scrapy与twisted

    笔记-scrapy与twisted Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码. 在任何情况下,都不要写阻塞的代码.阻塞的代码包括: ...

  3. Scrapy去重

    一.原生 1.模块 from scrapy.dupefilters import RFPDupeFilter 2.RFPDupeFilter方法 a.request_seen 核心:爬虫每执行一次yi ...

  4. python学习笔记 | 列表去重

    ''' @author: 人人都爱小雀斑 @time: 2020/3/10 10:29 @desc: ''' L=[1,5,7,4,6,3,0,5,8,4,4] 方法1:for循环 L1=[] for ...

  5. JS学习笔记——数组去重

    <script type="text/javascript"> //indexOf"是ECMAScript5方法,IE8以下不支持,需多写兼容低版本浏览器代码 ...

  6. scrapy 去重 dont_filter=False

    yield Request(...... dont_filter=False)

  7. scrapy暂停和重启,及url去重原理,telenet简单使用

    一.scrapy暂停与重启 1.要暂停,就要保留一些中间信息,以便重启读取中间信息并从当前位置继续爬取,则需要一个目录存放中间信息: scrapy crawl spider_name -s JOBDI ...

  8. Scrapy 初体验

    开发笔记 Scrapy 初体验 scrapy startproject project_name 创建工程 scrapy genspider -t basic spider_name website. ...

  9. Python Scrapy环境配置教程+使用Scrapy爬取李毅吧内容

    Python爬虫框架Scrapy Scrapy框架 1.Scrapy框架安装 直接通过这里安装scrapy会提示报错: error: Microsoft Visual C++ 14.0 is requ ...

随机推荐

  1. JQ单双引号转义

    var temp = "${row.address_province}"; alert(temp);——————即变量temp alert("\'"+temp+ ...

  2. (六)JavaScript之[Regular Expression]与[错误(try, catch, throw)]

    10].正则表达式 /** * 正则表达式(Regular Expression): * * 用于文本搜索和文本替换 * */ /** * /good/i是一个正则表达式. * good是一个模式(用 ...

  3. Implementation with Java

    Implementation with Java From:http://jcsc.sourceforge.net In general, follow the Sun coding conventi ...

  4. 【从业余项目中学习1】C# 实现XML存储用户名密码(MD5加密)

    最近在写一个C#的项目,用户需求是实现Winform的多文档界面与Matlab算法程序之间的交互.做了一段时间发现,这既能利用业余时间,实战中也可学习一些技术,同时刚毕业也增加一份收入.所以后面会不断 ...

  5. System Center Configuration Manager 2016 配置安装篇(Part3)

    SCCM 2016 配置管理系列(Part 1- 4) 介绍AD01上配置了Active Directory域服务(ADDS),然后将Configuration Manager服务器(CM16)加入到 ...

  6. Windows下hosts文件的作用

    原文地址:https://my.oschina.net/u/874225/blog/194348 在操作系统中的路径:Win7在C:\Windows\System32\drivers\etc目录下 内 ...

  7. "ssllabs" website and "testssl" website

    "https://www.ssllabs.com" could scan your server and give results about the weakness of yo ...

  8. Windows Host 文件

    Windows XP Home / Windows 7/ Windows Server 2008 c:\windows\system32\drivers\etc\hosts 如果碰到Localhost ...

  9. Torch.no_grad()影响MSE损失

    相关描述 https://discuss.pytorch.org/t/torch-no-grad-affecting-outputs-loss/28595/3 今天在训练网络的时候,发现mseloss ...

  10. python title() upper() lower() 以首字母大写的方式显示每个单词/将字符串改为全部大写或全部小写

    以首字母大写的方式显示每个单词 [root@chenbj python]# cat name.py #!/usr/bin/env python # _*_ coding:utf-8 _*_ name ...