笔记-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介绍 安装 基本命令 项目结构以及爬虫应用介绍 简单使用示例 选 ...
随机推荐
- C#中WinForm程序退出方法技巧总结[转]
这篇文章主要介绍了C#中WinForm程序退出方法,实例总结了技巧退出WinForm程序窗口的各种常用技巧,非常具有实用价值,需要的朋友可以参考下 本文实例总结了C#中WinForm程序退出方法技 ...
- C语言总结的知识点
什么是有效数字? ------------------------- 数据类型 运算符: 函数: 程序结构: 存储结构 内存管理 关键字: ------------------------- C语言: ...
- *521. Longest Uncommon Subsequence I (bit manipulation 2^n)
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...
- IOS SVN源代码管理工具使用
01. 源代码管理工具概述(PPT)===================================================* 源代码管理工具的作用:# 能追踪一个项目从诞生一直到 ...
- EF中 实现延迟加载 lazyload
1.创建数据库 2.利用数据库 生成视图 生成2个实体类 和一个model1类 3.写代码 (1) 创建 上下文对象 (2) (3)查询结果 注释: 延迟加载的原因,因为我们操作数据库不会那么简单, ...
- matlab linux下无界面运行
今日做吸引域的仿真,由于需要遍历100*100*100的空间,需要的时间比较长,发现程序没运行一段时间,就会出现Out of memory的错误,而且出错的部分在于截取figure内部图片的部分. 开 ...
- (转载)git常用命令
创建和使用git ssh key 首先设置git的user name和email: git config --global user.name "xxx" git config - ...
- Firefox 修改User Agent
Android 版 Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML ...
- 第44章 MPU6050传感器—姿态检测—零死角玩转STM32-F429系列
第44章 MPU6050传感器—姿态检测 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.co ...
- PHP调试小错误汇总
1.中文显示乱码 解决:加上header('Content-type:text'); 2.Unable to find the wrapper "https 解决:到php.ini中把ext ...