笔记-scrapy-signal
笔记-scrapy-signal
1. scrapy singal
1.1. 信号机制
scrapy的信号机制主要由三个模块完成
signals.py 定义信号量
signalmanager.py 管理
utils/signal.py 真正干活的
scrapy自带一些内置的信号,定义在signals.py下:
engine_started = object()
engine_stopped = object()
spider_opened = object()
spider_idle = object()
spider_closed = object()
spider_error = object()
request_scheduled = object()
request_dropped = object()
response_received = object()
response_downloaded = object()
item_scraped = object()
item_dropped = object()
# for backwards compatibility
stats_spider_opened = spider_opened
stats_spider_closing = spider_closed
stats_spider_closed = spider_closed
item_passed = item_scraped
request_received = request_scheduled
scrapy定义了这些信号,并在相关时刻触发信号,下面就是其中一个案例:
yield self.signals.send_catch_log_deferred(signal=signals.engine_started)
至于这些信号的含义和触发时刻参考文档:https://docs.scrapy.org/en/latest/topics/signals.html
1.2. scrapy信号使用
scrapy已经定义了常用的信号,开发人员可以在扩展类/spider/pipeline中对这些信号做关联。
下面是一个扩展类中使用信号的例子:spider_open_s.py
#coding:utf-8
import logging
from scrapy import signals
logger = logging.getLogger(__name__)
class spider_open(object):
@classmethod
def from_crawler(cls, crawler):
ext = cls()
crawler.signals.connect(ext.spider_open_log, signal=signals.spider_opened)
return ext
def spider_open_log(self, spider):
logger.info('spider is opened!')
input('input a number to go on:')
非常简单,希望在spider打开后有一个提示或操作,那么在扩展类中将spider_opened信号与要进行的操作函数关联起来,scrapy在初始化spider时会触发spider_opened信号,然后执行关联的函数。
1.3. signal深入
scrapy的信号处理底层使用的是dispatch模块:
from pydispatch import dispatcher
如果想要更细致的操作信号,scrapy也提供了接口,scrapy是通过signalmanager类操作信号的:
classscrapy.signalmanager.SignalManager(sender=_Anonymous)
常用方法
- connect(receiver, signal, **kwargs)
Connect a receiver function to a signal.
The signal can be any object, although Scrapy comes with some predefined signals that are documented in the Signals section.
|
Parameters: |
receiver (callable) – the function to be connected signal (object) – the signal to connect to |
- disconnect(receiver, signal, **kwargs)
Disconnect a receiver function from a signal. This has the opposite effect of the connect()method, and the arguments are the same.
- disconnect_all(signal, **kwargs)
Disconnect all receivers from the given signal.
|
Parameters: |
signal (object) – the signal to disconnect from |
- send_catch_log(signal, **kwargs)
Send a signal, catch exceptions and log them.
The keyword arguments are passed to the signal handlers (connected through the connect()method).
- send_catch_log_deferred(signal, **kwargs)
Like send_catch_log() but supports returning deferreds from signal handlers.
Returns a Deferred that gets fired once all signal handlers deferreds were fired. Send a signal, catch exceptions and log them.
The keyword arguments are passed to the signal handlers (connected through the connect()method).
笔记-scrapy-signal的更多相关文章
- 笔记-scrapy与twisted
笔记-scrapy与twisted Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码. 在任何情况下,都不要写阻塞的代码.阻塞的代码包括: ...
- python学习笔记——信号模块signal
基于python学习笔记——多进程间通信——Linux信号基础的学习基础,进一步学习Python标准库中的signal模块. 尽管signal是python中的模块,但是主要针对UNIX平台(比如Li ...
- 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 ...
- signal 信号
python学习笔记--信号模块signal 阅读目录(Content) 1 signal基本信号名 2 常用信号处理函数 2.1 设置发送SIGALRM信号的定时器 2.2 设置信号处理函数 3 常 ...
- scrapy-redis源码浅析
原文链接 前言 分析这个项目的源码原因是需要有去重过滤,增量爬取两个功能,而scrapy-redis项目已经帮我们实现了,想看看他是怎么实现的.这里只贴出部分主要代码,查看时请打开源码对照,笔记有点长 ...
- python数据类
前言 之前有写过一篇python元类的笔记,元类主要作用就是在要创建的类中使用参数metaclass=YourMetaclass调用自定义的元类,这样就可以为所有调用了这个元类的类添加相同的属性了. ...
- python内置装饰器
前言 接着上一篇笔记,我们来看看内置装饰器property.staticmethod.classmethod 一.property装饰器 1. 普通方式修改属性值 code class Celsius ...
- scrapy-redis debug视频
前言 在上一篇笔记说过会录个视频帮助理解里面的类方法,现在视频来了.只录了debug scheduler.py里面的类方法,还有spiders.py里面的类方法差不多,就不说了,自己动手丰衣足食.限于 ...
- scrapy笔记集合
细读http://scrapy-chs.readthedocs.io/zh_CN/latest/index.html 目录 Scrapy介绍 安装 基本命令 项目结构以及爬虫应用介绍 简单使用示例 选 ...
随机推荐
- canvas的arcTo API
- 秒懂JSON.parse()与JSON.stringify()的区别
在网站开发中,Json是最为常见的一种数据交互手段.在使用过程中,常会遇到Json字段串和对象之间进行转换.很多朋友对于JSON.parse() 和JSON.stringify() 的区别,下面为大家 ...
- (WPF) DataGrid之绑定
通过ObservableCollection 绑定到 DataGrid. 1. 前台Xaml. <DataGrid x:Name="dgMeasurements" Horiz ...
- 【FPGA】Quartus导出.qxp格式的网表文件
首先,右击项目顶层文件. 选择Design Partition -> Export Design Partition 即可完成.
- 通过windows计划任务和Dos批处理备份文件
目的: 1.计划每天每半小时备份1次,每天8点开始,执行12小时,20点结束. 2.定期删除历史备份文件,由于每天有多个时间段备份,删除前只保留当天最后一个备份. 说明: 由于删除的操作只有每天第一次 ...
- 笨办法学Python(十一)
习题 11: 提问 我已经出过很多打印相关的练习,让你习惯写简单的东西,但简单的东西都有点无聊,现在该跟上脚步了.我们现在要做的是把数据读到你的程序里边去.这可能对你有点难度,你可能一下子不明白,不过 ...
- 在你的andorid设备上运行netcore (Linux Deploy)
最近注意到.net core 的新版本已经开始支持ARM 平台的CPU, 特意去Linux Deploy 中尝试了一下,真的可以运行 Welcome to Ubuntu 16.04 LTS (GNU/ ...
- mysql索引长度
http://blog.csdn.net/qsc0624/article/details/51335632 大家应该知道InnoDB单列索引长度不能超过767bytes,联合索引还有一个限制是长度不能 ...
- _default_ VirtualHost overlap on port 80, the first has precedence
去掉#NameVirtualHost *:80,然后重启httpd
- HTML第一章:初始HTML
设置ws字体大小:左上角file-->Settings--->在搜索框中输入font 网页的第一行一定是<!DOCTYPE html>:网页声明,代表这个页面是h5页面html ...