笔记-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介绍 安装 基本命令 项目结构以及爬虫应用介绍 简单使用示例 选 ...
随机推荐
- 【Android 界面效果47】RecyclerView详解
RecylerView作为 support-library发布出来,这对开发者来说绝对是个好消息.因为可以在更低的Android版本上使用这个新视图.下面我们看如何获取 RecylerView.首先打 ...
- JDBC中重要的类/接口-Connection、DriverManager、ResultSet、Statement及常用方法
DriverManager(管理一组 JDBC 驱动程序的基本服务) 它的方法: getConnection(String url, String user, String password) 试图建 ...
- MySQL的基础(优化)1
1,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽可能小 2,在可能的情况下,应该尽量把字段设置为NOT NULL,这样在将来执行查询的时候,数据库不用去比较NULL值 3,对于某 ...
- To my dear friends in SFAE
To my dear friends in SFAE, 这不是farewell,我还在西门子大家庭.2018年1月份我会转到SLC MCBU.在SFAE十年,一些敢想,唠叨唠叨~ 十年弹指一挥间.记得 ...
- mybatis怎样自动生成java类,配置文件?
其实没有什么东西是可以自动生成的,只不过是别人已经写好了,你调用罢了. 所以想要mybatis自动生成java类,配置文件等,就必须要一些配置和一些jar包.当然这些配置也很简单. 为了有个初步的认识 ...
- 类似LIS+贪心(ZOJ1025)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=25 解题报告: #include <cstdio> #in ...
- spring boot学习1
转:https://blog.csdn.net/jsyxcjw/article/details/46763639 1 开始 1.1 spring介绍 Spring Boot使开发独立的,产品级别的基于 ...
- MapReduce执行jar练习
1.用程序生成输入文件1.txt和2.txt 生成程序源码如下: https://www.cnblogs.com/jonban/p/10555364.html 2. 上传文件到hdfs文件系统 创建 ...
- javaweb基础(37)_mysql数据库自动生成主键
测试脚本如下: 1 create table test1 2 ( 3 id int primary key auto_increment, 4 name varchar(20) 5 ); 测试代码: ...
- HTTP 之缓存
这是一篇知识性的文档,主要目的是为了让Web缓存相关概念更容易被开发者理解并应用于实际的应用环境中.为了简要起见,某些实现方面的细节被简化或省略了.如果你更关心细节实现则完全不必耐心看完本文,后面参考 ...