Extensions
Extensions are just regular classes.
Extensions use the Scrapy settings to manage their settings, just like any other Scrapy code.
It is customary for extensions to prefix their settings with their own name, to avoid collision with existing (and future) extensions. For example, a hypothetic extension to handle would use settings like , GOOGLESITEMAP_DEPTH
, and so on.
Loading & activating extensions
Extensions are loaded and activated at startup by instantiating a single instance of the extension class per spider being run. All the extension initialization code must be performed in the class __init__
method.
To make an extension available, add it to the setting in your Scrapy settings. In EXTENSIONS, each extension is represented by a string: the full Python path to the extension’s class name. For example:
As you can see, the setting is a dict where the keys are the extension paths, and their values are the orders, which define the extension loading order. The EXTENSIONS setting is merged with the setting defined in Scrapy (and not meant to be overridden) and then sorted by order to get the final sorted list of enabled extensions.
As extensions typically do not depend on each other, their loading order is irrelevant in most cases. This is why the EXTENSIONS_BASE setting defines all extensions with the same order (0
). However, this feature can be exploited if you need to add an extension which depends on other extensions already loaded.
Not all available extensions will be enabled. Some of them usually depend on a particular setting. For example, the HTTP Cache extension is available by default but disabled unless the setting is set.
Disabling an extension
In order to disable an extension that comes enabled by default (i.e. those included in the setting) you must set its order to None
. For example:
Each extension is a Python class. The main entry point for a Scrapy extension (this also includes middlewares and pipelines) is the from_crawler
class method which receives a instance. Through the Crawler object you can access settings, signals, stats, and also control the crawling behaviour.
Typically, extensions connect to signals and perform tasks triggered by them.
Finally, if the from_crawler
method raises the exception, the extension will be disabled. Otherwise, the extension will be enabled.
Here we will implement a simple extension to illustrate the concepts described in the previous section. This extension will log a message every time:
a spider is opened
a spider is closed
a specific number of items are scraped
The extension will be enabled through the MYEXT_ENABLED
setting and the number of items will be specified through the MYEXT_ITEMCOUNT
setting.
Here is the code of such extension:
Built-in extensions reference
Log Stats extension
class scrapy.extensions.logstats.LogStats[source]
Log basic stats like crawled pages and scraped items.
Core Stats extension
class scrapy.extensions.corestats.CoreStats[source]
Enable the collection of core statistics, provided the stats collection is enabled (see ).
Telnet console extension
class scrapy.extensions.telnet.TelnetConsole
Provides a telnet console for getting into a Python interpreter inside the currently running Scrapy process, which can be very useful for debugging.
Memory usage extension
class scrapy.extensions.memusage.MemoryUsage
Note
This extension does not work in Windows.
Monitors the memory used by the Scrapy process that runs the spider and:
sends a notification e-mail when it exceeds a certain value
closes the spider when it exceeds a certain value
The notification e-mails can be triggered when a certain warning value is reached (MEMUSAGE_WARNING_MB) and when the maximum value is reached () which will also cause the spider to be closed and the Scrapy process to be terminated.
This extension is enabled by the MEMUSAGE_ENABLED setting and can be configured with the following settings:
Memory debugger extension
class scrapy.extensions.memdebug.MemoryDebugger
An extension for debugging memory usage. It collects information about:
objects uncollected by the Python garbage collector
objects left alive that shouldn’t. For more info, see Debugging memory leaks with trackref
To enable this extension, turn on the setting. The info will be stored in the stats.
Close spider extension
class scrapy.extensions.closespider.CloseSpider
Closes a spider automatically when some conditions are met, using a specific closing reason for each condition.
The conditions for closing a spider can be configured through the following settings:
When a certain closing condition is met, requests which are currently in the downloader queue (up to CONCURRENT_REQUESTS requests) are still processed.
CLOSESPIDER_TIMEOUT
Default: 0
An integer which specifies a number of seconds. If the spider remains open for more than that number of second, it will be automatically closed with the reason closespider_timeout
. If zero (or non set), spiders won’t be closed by timeout.
CLOSESPIDER_ITEMCOUNT
Default:
An integer which specifies a number of items. If the spider scrapes more than that amount and those items are passed by the item pipeline, the spider will be closed with the reason closespider_itemcount
. If zero (or non set), spiders won’t be closed by number of passed items.
CLOSESPIDER_PAGECOUNT
Default: 0
An integer which specifies the maximum number of responses to crawl. If the spider crawls more than that, the spider will be closed with the reason closespider_pagecount
. If zero (or non set), spiders won’t be closed by number of crawled responses.
CLOSESPIDER_ERRORCOUNT
Default: 0
An integer which specifies the maximum number of errors to receive before closing the spider. If the spider generates more than that number of errors, it will be closed with the reason closespider_errorcount
. If zero (or non set), spiders won’t be closed by number of errors.
StatsMailer extension
class scrapy.extensions.statsmailer.StatsMailer[source]
This simple extension can be used to send a notification e-mail every time a domain has finished scraping, including the Scrapy stats collected. The email will be sent to all recipients specified in the setting.
Emails can be sent using the MailSender class. To see a full list of parameters, including examples on how to instantiate and use mail settings, see Sending e-mail.
Stack trace dump extension
class scrapy.extensions.debug.StackTraceDump[source]
Dumps information about the running process when a or SIGUSR2 signal is received. The information dumped is the following:
engine status (using )
live references (see )
stack trace of all threads
After the stack trace and engine status is dumped, the Scrapy process continues running normally.
This extension only works on POSIX-compliant platforms (i.e. not Windows), because the SIGQUIT and signals are not available on Windows.
There are at least two ways to send Scrapy the SIGQUIT signal:
By pressing Ctrl-while a Scrapy process is running (Linux only?)
Debugger extension
class scrapy.extensions.debug.Debugger[source]
Invokes a inside a running Scrapy process when a SIGUSR2 signal is received. After the debugger is exited, the Scrapy process continues running normally.
This extension only works on POSIX-compliant platforms (i.e. not Windows).