mirror of https://github.com/scrapy/scrapy.git
Merge pull request #1566 from darshanime/signal-example
[MRG+1] Include example for signal docs
This commit is contained in:
commit
b6375d3701
|
|
@ -16,6 +16,37 @@ deliver the arguments that the handler receives.
|
|||
You can connect to signals (or send your own) through the
|
||||
:ref:`topics-api-signals`.
|
||||
|
||||
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
|
||||
========================
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
"""Helper functions which doesn't fit anywhere else"""
|
||||
"""Helper functions which don't fit anywhere else"""
|
||||
import re
|
||||
import hashlib
|
||||
from importlib import import_module
|
||||
|
|
|
|||
Loading…
Reference in New Issue