信号(Signals)

Scrapy使用信号来通知事情发生。 您可以在您的Scrapy项目中捕捉一些信号(使用 extension)来完成额外的工作或添加额外的功能,扩展Scrapy。

虽然信号提供了一些参数,不过处理函数不用接收所有的参数 - 信号分发机制(singal dispatching mechanism)仅仅提供处理器(handler)接受的参数。

您可以通过 信号(Signals) API 来连接(或发送您自己的)信号。

Here is a simple example showing how you can catch signals and perform some action:

from scrapy import signals
from scrapy import Spider


class DmozSpider(Spider):
    name = "dmoz"
    allowed_domains = ["dmoz.org"]
    start_urls = [
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/",
    ]


    @classmethod
    def from_crawler(cls, crawler, *args, **kwargs):
        spider = super(DmozSpider, cls).from_crawler(crawler, *args, **kwargs)
        crawler.signals.connect(spider.spider_closed, signal=signals.spider_closed)
        return spider


    def spider_closed(self, spider):
        spider.logger.info('Spider closed: %s', spider.name)


    def parse(self, response):
        pass

延迟的信号处理器(Deferred signal handlers)

有些信号支持从处理器返回Twisted deferreds,参考下边的内置信号参考来了解哪些支持。

内置信号参考手册(Built-in signals reference)

以下给出Scrapy内置信号的列表及其意义。

engine_started

scrapy.signals.engine_started()

当Scrapy引擎启动爬取时发送该信号。

该信号支持返回deferreds。

Note

该信号可能会在信号spider_opened被发送,取决于spider的启动方式。所以不要依赖 该信号会比spider-opened更早被发送。

engine_stopped

scrapy.signals.engine_stopped()

当Scrapy引擎停止时发送该信号(例如,爬取结束)。

该信号支持返回deferreds。

item_scraped

scrapy.signals.item_scraped(item, response, spider)

当item被爬取,并通过所有 Item Pipeline 后(没有被丢弃(dropped),发送该信号。

该信号支持返回deferreds。

Parameters:
  • item (Item 对象) – 爬取到的item
  • spider (Spider object) – the spider which scraped the item
  • response (Response 对象) – 提取item的response

item_dropped

scrapy.signals.item_dropped(item, response, exception, spider)

当item通过 Item Pipeline ,有些pipeline抛出 DropItem 异常,丢弃item时,该信号被发送。

该信号支持返回deferreds。

Parameters:
  • item (Item 对象) – Item Pipeline 丢弃的item
  • spider (Spider object) – the spider which scraped the item
  • response (Response 对象) – 下载的response
  • exception (DropItem 异常) – 导致item被丢弃的异常(必须是 DropItem 的子类)

spider_closed

scrapy.signals.spider_closed(spider, reason)

当某个spider被关闭时,该信号被发送。 该信号可以用来释放每个spider在 spider_opened 时占用的资源。

该信号支持返回deferreds。

Parameters:
  • spider (Spider object) – the spider which has been closed
  • reason (str) – 描述spider被关闭的原因的字符串。 如果spider是由于完成爬取而被关闭,则其为 'finished' 否则,如果spider是被引擎的 close_spider 方法所关闭,则其为调用该方法时传入的 reason 参数(默认为 'cancelled')。 如果引擎被关闭(例如, 输入Ctrl-C),则其为 'shutdown'

spider_opened

scrapy.signals.spider_opened(spider)

当spider开始爬取时发送该信号。 该信号一般用来分配spider的资源,不过其也能做任何事。

该信号支持返回deferreds。

Parameters:spider (Spider object) – the spider which has been opened

spider_idle

scrapy.signals.spider_idle(spider)

当spider进入空闲(idle)状态时该信号被发送,空闲意味着:

  • requests正在等待被下载
  • requests被调度
  • items正在item pipeline中被处理

当该信号的所有处理器(handler)被调用后,如果spider仍然保持空闲状态, 引擎将会关闭该spider。 当spider被关闭后, spider_closed 信号将被发送。

您可以,比如,在 spider_idle 处理器中调度某些请求来避免spider被关闭。

该信号 不支持从它们的处理器返回deferreds。

Parameters:spider (Spider object) – the spider which has gone idle

spider_error

scrapy.signals.spider_error(failure, response, spider)

当spider的回调函数产生错误时,该信号被发送(例如,抛出异常)。

This signal does not support returning deferreds from their handlers.

Parameters:
  • failure (Failure object) – the exception raised as a Twisted Failure object
  • response (Response 对象) – 当异常被抛出时被处理的response
  • spider (Spider object) – the spider which raised the exception

request_scheduled

scrapy.signals.request_scheduled(request, spider)

当引擎调度一个 Request 对象用于下载时,该信号被发送。

该信号 不支持 返回deferreds。

Parameters:
  • request (Request 对象) – 到达调度器的request
  • spider (Spider object) – the spider that yielded the request

request_dropped

scrapy.signals.request_dropped(request, spider)

Sent when a Request, scheduled by the engine to be downloaded later, is rejected by the scheduler.

The signal does not support returning deferreds from their handlers.

Parameters:
  • request (Request 对象) – 生成response的request
  • spider (Spider object) – the spider that yielded the request

response_received

scrapy.signals.response_received(response, request, spider)

当引擎从downloader获取到一个新的 Response 时发送该信号。

该信号 不支持 返回deferreds。

Parameters:
  • response (Response 对象) – 接收到的response
  • request (Request 对象) – 生成response的request
  • spider (Spider object) – the spider for which the response is intended

response_downloaded

scrapy.signals.response_downloaded(response, request, spider)

当一个 HTTPResponse 被下载时,由downloader发送该信号。

该信号 不支持 返回deferreds。

Parameters:
  • response (Response object) – the response downloaded
  • request (Request object) – the request that generated the response
  • spider (Spider object) – the spider for which the response is intended