Scrapy provides a convenient service for collecting stats in the form of key/values, both globally and per spider. It’s called the Stats Collector, and it’s a singleton which can be imported and used quickly, as illustrated by the examples in the Common Stats Collector uses section below.
The stats collection is enabled by default but can be disabled through the STATS_ENABLED setting.
However, the Stats Collector is always available, so you can always import it in your module and use its API (to increment or set new stat keys), regardless of whether the stats collection is enabled or not. If it’s disabled, the API will still work but it won’t collect anything. This is aimed at simplifying the stats collector usage: you should spend no more than one line of code for collecting stats in your spider, Scrapy extension, or whatever code you’re using the Stats Collector from.
Another feature of the Stats Collector is that it’s very efficient (when enabled) and extremely efficient (almost unnoticeable) when disabled.
The Stats Collector keeps one stats table per open spider and one global stats table. You can’t set or get stats from a closed spider, but the spider-specific stats table is automatically opened when the spider is opened, and closed when the spider is closed.
Import the stats collector:
from scrapy.stats import stats
Set global stat value:
stats.set_value('hostname', socket.gethostname())
Increment global stat value:
stats.inc_value('spiders_crawled')
Set global stat value only if greater than previous:
stats.max_value('max_items_scraped', value)
Set global stat value only if lower than previous:
stats.min_value('min_free_memory_percent', value)
Get global stat value:
>>> stats.get_value('spiders_crawled')
8
Get all global stats (ie. not particular to any spider):
>>> stats.get_stats()
{'hostname': 'localhost', 'spiders_crawled': 8}
Set spider specific stat value (spider stats must be opened first, but this task is handled automatically by the Scrapy engine):
stats.set_value('start_time', datetime.now(), spider=some_spider)
Where some_spider is a BaseSpider object.
Increment spider-specific stat value:
stats.inc_value('pages_crawled', spider=some_spider)
Set spider-specific stat value only if greater than previous:
stats.max_value('max_items_scraped', value, spider=some_spider)
Set spider-specific stat value only if lower than previous:
stats.min_value('min_free_memory_percent', value, spider=some_spider)
Get spider-specific stat value:
>>> stats.get_value('pages_crawled', spider=some_spider)
1238
Get all stats from a given spider:
>>> stats.get_stats('pages_crawled', spider=some_spider)
{'pages_crawled': 1238, 'start_time': datetime.datetime(2009, 7, 14, 21, 47, 28, 977139)}
There are several Stats Collectors available under the scrapy.stats.collector module and they all implement the Stats Collector API defined by the StatsCollector class (which they all inherit from).
Return a iterator over (spider, spider_stats) for each open spider currently tracked by the stats collector, where spider_stats is the dict containing all spider-specific stats.
Global stats are not included in the iterator. If you want to get those, use get_stats() method.
Besides the basic StatsCollector there are other Stats Collectors available in Scrapy which extend the basic Stats Collector. You can select which Stats Collector to use through the STATS_CLASS setting. The default Stats Collector is the MemoryStatsCollector is used.
When stats are disabled (through the STATS_ENABLED setting) the STATS_CLASS setting is ignored and the DummyStatsCollector is used.
A simple stats collector that keeps the stats of the last scraping run (for each spider) in memory, after they’re closed. The stats can be accessed through the domain_stats attribute, which is a dict keyed by spider domain name.
This is the default Stats Collector used in Scrapy.
A Stats collector which persists stats to Amazon SimpleDB, using one SimpleDB item per scraping run (ie. it keeps history of all scraping runs). The data is persisted to the SimpleDB domain specified by the STATS_SDB_DOMAIN setting. The domain will be created if it doesn’t exist.
In addition to the existing stats keys the following keys are added at persitance time:
- domain: the spider domain (so you can use it later for querying stats for that domain)
- timestamp: the timestamp when the stats were persisited
Both the domain and timestamp are used for generating the SimpleDB item name in order to avoid overwriting stats of previous scraping runs.
As required by SimpleDB, datetime’s are stored in ISO 8601 format and numbers are zero-padded to 16 digits. Negative numbers are not currently supported.
This Stats Collector requires the boto library.
This Stats Collector can be configured through the following settings:
Default: 'scrapy_stats'
A string containing the SimpleDB domain to use in the SimpledbStatsCollector.
Default: False
If True communication with SimpleDB will be performed asynchronously. If False blocking IO will be used instead. This is the default as using asynchronous communication can result in the stats not being persisted if the Scrapy engine is shut down in the middle (for example, when you run only one spider in a process and then exit).
The Stats Collector provides some signals for extending the stats collection functionality:
Sent right after the stats spider is opened. You can use this signal to add startup stats for spider (example: start time).
Parameter: | spider (str) – the stats spider just opened |
---|
Sent just before the stats spider is closed. You can use this signal to add some closing stats (example: finish time).
Parameters: |
|
---|
Sent right after the stats spider is closed. You can use this signal to collect resources, but not to add any more stats as the stats spider has already been close (use stats_spider_closing for that instead).
Parameters: |
|
---|