笔记-scrapy-辅助功能
笔记-scrapy-辅助功能
1. scrapy爬虫管理
爬虫主体写完了,要部署运行,还有一些工程性问题;
- 限频
- 爬取深度限制
- 按条件停止,例如爬取次数,错误次数;
- 资源使用限制,例如内存限制;
- 状态报告,邮件
- 性能问题。
2. 限频
CONCURRENT_ITEMS # item队列最大容量
Default: 100
Maximum number of concurrent items (per response) to process in parallel in the Item Processor (also known as the Item Pipeline).
CONCURRENT_REQUESTS
Default: 16
The maximum number of concurrent (ie. simultaneous) requests that will be performed by the Scrapy downloader.
实现原理:
next_request中while循环,调用needs_back,如果len(self.active)>它,则继续空循环,否则下一步;
核心还是engine.py,会查询downloader,slot等是否需要等待
CONCURRENT_REQUESTS_PER_DOMAIN
Default: 8
The maximum number of concurrent (ie. simultaneous) requests that will be performed to any single domain.
See also: AutoThrottle extension and its AUTOTHROTTLE_TARGET_CONCURRENCY option.
CONCURRENT_REQUESTS_PER_IP
Default: 0
The maximum number of concurrent (ie. simultaneous) requests that will be performed to any single IP. If non-zero, the CONCURRENT_REQUESTS_PER_DOMAIN setting is ignored, and this one is used instead. In other words, concurrency limits will be applied per IP, not per domain.
This setting also affects DOWNLOAD_DELAY and AutoThrottle extension: if CONCURRENT_REQUESTS_PER_IP is non-zero, download delay is enforced per IP, not per domain.
DOWNLOAD_DELAY 下载间隔时间
3. 爬取深度限制
DEPTH_LIMIT
4. 按条件停止爬虫
首先,scrapy自带关闭扩展,可以在setting中设置自动关闭条件。
python3.6.4\Lib\site-packages\scrapy\extensions
核心就这么四句
crawler.signals.connect(self.error_count, signal=signals.spider_error)
crawler.signals.connect(self.page_count, signal=signals.response_received)
crawler.signals.connect(self.spider_opened, signal=signals.spider_opened)
crawler.signals.connect(self.item_scraped, signal=signals.item_scraped)
把信号connect到方法,每次达到条件就触发相应信号,到这里调用相应函数,完成操作。
操作函数示例:
def error_count(self, failure, response, spider):
self.counter['errorcount'] += 1
if self.counter['errorcount'] == self.close_on['errorcount']:
self.crawler.engine.close_spider(spider, 'closespider_errorcount')
当然,也可以自行判断然后抛出异常,调用已有关闭方法;
在spider中:raise CloseSpider(‘bandwidth_exceeded’)
在其它组件中(middlewares, pipeline, etc):
crawler.engine.close_spider(self, ‘log message’)
5. 资源使用限制
主要是内存
MEMDEBUG_ENABLED
Default: False
Whether to enable memory debugging.
MEMDEBUG_NOTIFY
Default: []
When memory debugging is enabled a memory report will be sent to the specified addresses if this setting is not empty, otherwise the report will be written to the log.
Example:MEMDEBUG_NOTIFY = ['user@example.com']
MEMUSAGE_LIMIT_MB
Default: 0
Scope: scrapy.extensions.memusage
The maximum amount of memory to allow (in megabytes) before shutting down Scrapy (if MEMUSAGE_ENABLED is True). If zero, no check will be performed.
MEMUSAGE_CHECK_INTERVAL_SECONDS
New in version 1.1.
Default: 60.0
Scope: scrapy.extensions.memusage
MEMUSAGE_NOTIFY_MAIL
Default: False
Scope: scrapy.extensions.memusage
A list of emails to notify if the memory limit has been reached.
Example:
MEMUSAGE_NOTIFY_MAIL = ['user@example.com']
MEMUSAGE_WARNING_MB
Default: 0
Scope: scrapy.extensions.memusage
The maximum amount of memory to allow (in megabytes) before sending a warning email notifying about it. If zero, no warning will be produced.
6. 状态监控及控制
这个得结合一般爬虫分布爬取任务划分状态来说,
大部分一定规模的爬虫框架都是一个主爬虫+多个子爬虫,主爬虫负责爬取列表页并从列表页中获取子页地址,生成二级请求队列;子爬虫负责从二级请求队列中读取请求,完成爬取。
爬虫监控信息及作用主要包括三种:
- 爬虫状态:包括爬虫已运行时间,已下载网页数,已下载目标数,请求队列大小,已发生错误数;
用于判断当前爬虫进度
- 爬虫进程状态:
进程监控:爬虫进程挂了,需要重启;
资源监控:爬虫使用资源超标,需要重启释放;
- 其它爬虫异常状态异常;
主要是异常停止或被ban,需要人工干预;
总体来说,监控日志监控是比较方便的方法,把日志写入数据库或使用sockethandler,
然后另写功能模块负责完成监控,展示,处理功能,可以使爬虫主体功能简单化,另一方面可以避开多进程写日志调度这个麻烦。
7. 邮件
from scrapy.mail import MailSender
mailer = MailSender(
smtphost = "smtp.163.com", # 发送邮件的服务器
mailfrom = "***********@163.com", # 邮件发送者
smtpuser = "***********@163.com", # 用户名
smtppass = "***********", # 发送密码不是登陆密码,而是授权码!
smtpport = 25 # 端口号
)
body = u"""发送的邮件内容"""
subject = u'发送的邮件标题'
# 如果发送的内容太过简单的话,很可能会部分邮箱的反垃圾设置给禁止
mailer.send(to=["****@qq.com", "****@qq.com"], subject = subject.encode("utf-8"), body = body.encode("utf-8"))能会被当做垃圾邮件给禁止发送。
8. 性能问题
性能问题有原因有多种:
- 并发数太小,未充分使用CPU,网络及IO,扩大并发数就可以了;把单IP或单域名的并发数改大;
- 网络,磁盘io,这个只能升级或分布式;一般够得上这种问题的爬虫规模小不了,也只能分布式了;不过这样也太嚣张了,容易被反爬;
- CPU
在爬虫中,CPU最容易出现瓶颈的地方是解析,
纵向扩展是提高解析效率,使用更高效率的库,或条件允许直接上正则;另外scrapy在解析时是单线程的,可以考虑使用gevent;
横向扩展是多进程,在一台服务器上同时运行多个爬虫;
笔记-scrapy-辅助功能的更多相关文章
- 笔记-scrapy与twisted
笔记-scrapy与twisted Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码. 在任何情况下,都不要写阻塞的代码.阻塞的代码包括: ...
- Scrapy 初体验
开发笔记 Scrapy 初体验 scrapy startproject project_name 创建工程 scrapy genspider -t basic spider_name website. ...
- Python Scrapy环境配置教程+使用Scrapy爬取李毅吧内容
Python爬虫框架Scrapy Scrapy框架 1.Scrapy框架安装 直接通过这里安装scrapy会提示报错: error: Microsoft Visual C++ 14.0 is requ ...
- kindle使用参考
转载链接:http://blog.sina.com.cn/nuanfengjia 今天买的kindle499刚刚到货了,体验略差,还有一个就是无按键,完全不会玩,只能自己慢慢摸索了. [新Kindle ...
- python数据类
前言 之前有写过一篇python元类的笔记,元类主要作用就是在要创建的类中使用参数metaclass=YourMetaclass调用自定义的元类,这样就可以为所有调用了这个元类的类添加相同的属性了. ...
- python内置装饰器
前言 接着上一篇笔记,我们来看看内置装饰器property.staticmethod.classmethod 一.property装饰器 1. 普通方式修改属性值 code class Celsius ...
- scrapy-redis debug视频
前言 在上一篇笔记说过会录个视频帮助理解里面的类方法,现在视频来了.只录了debug scheduler.py里面的类方法,还有spiders.py里面的类方法差不多,就不说了,自己动手丰衣足食.限于 ...
- scrapy-redis源码浅析
原文链接 前言 分析这个项目的源码原因是需要有去重过滤,增量爬取两个功能,而scrapy-redis项目已经帮我们实现了,想看看他是怎么实现的.这里只贴出部分主要代码,查看时请打开源码对照,笔记有点长 ...
- scrapy爬虫笔记(一)------环境配置
前言: 本系列文章是对爬虫的简单介绍,以及教你如何用简单的方法爬取网站上的内容. 需要阅读者对html语言及python语言有基本的了解. (本系列文章也是我在学习爬虫过程中的学习笔记,随着学习的深入 ...
- Learning Scrapy笔记(六)- Scrapy处理JSON API和AJAX页面
摘要:介绍了使用Scrapy处理JSON API和AJAX页面的方法 有时候,你会发现你要爬取的页面并不存在HTML源码,譬如,在浏览器打开http://localhost:9312/static/, ...
随机推荐
- 电路中GND和GROUND、VCC,VDD,VEE,VSS
电路解析:GND和GROUND.VCC,VDD,VEE,VSS 参考: http://www.elecfans.com/dianzichangshi/20160822432514.html 一.解释版 ...
- (C# 基础) 静态字段,静态类,静态方法。
静态字段被类的所有实例所共享,即此类的所有实例都访问同一内存地址. 所以该内存位置的值变更的话,这种变更对所有的实例都可见. class MyClass { ; ; public void SetVa ...
- 《ArcGIS Runtime SDK for Android开发笔记》——(6)、基于Android Studio的ArcGIS Android工程结构解析
1.前言 Android Studio 是第一个Google官方的 Android 开发环境.其他工具,例如 Eclipse,在 Android Studio 发布之前已经有了大规模的使用.为了帮助开 ...
- check_mk插件 redis
/usr/lib/check_mk_agent/plugins/mk_redis #!/bin/bash echo '<<<redis>>>' hosts=$(ne ...
- Linux命令之查看服务进程(ps aux、ps -aux、ps -ef)的运用
执行ps命令即可列出的是当前服务器进程的快照(时间点),如果想要实时动态的显示进程信息,就可以使用top命令. linux上进程有5种状态: 1. 运行(正在运行或在运行队列中等待) 2. 中断( ...
- CentOS6.8上安装epel
实验环境: [root@bogon zhi]# uname -a Linux bogon -.el6.i686 # SMP Fri Nov :: UTC i686 i686 i386 GNU/Linu ...
- 154. Find Minimum in Rotated Sorted Array II(Binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follo ...
- 起一个node服务
使用node开发一个应用,非常简单,甚至都不用去配置一堆文件来启动一个webu服务器,直接去官网把这一段示例代码拷过来 https://nodejs.org/en/about/ 中文网没有这个abou ...
- Next K Permutation
3457: Next K Permutation 时间限制: 1 Sec 内存限制: 128 MB提交: 4 解决: 4[提交] [状态] [讨论版] [命题人:admin] 题目描述 n 个数有 ...
- linq 查询的两种方法 (在EF model中实现)
众所周知:linq查询有两种方式 1.通过linq表达式查询 2.是通过linq方法查询 代码中 每一步都有注释