2.7 KiB
Stats Collection
Scrapy provides a convenient facility for collecting stats in the form of key/value pairs, where values are often counters. The facility is called the Stats Collector, and can be accessed through the :attr:`~scrapy.crawler.Crawler.stats` attribute of the :ref:`topics-api-crawler`, as illustrated by the examples in the :ref:`topics-stats-usecases` section below.
System Message: ERROR/3 (<stdin>, line 7); backlink
Unknown interpreted text role "attr".System Message: ERROR/3 (<stdin>, line 7); backlink
Unknown interpreted text role "ref".System Message: ERROR/3 (<stdin>, line 7); backlink
Unknown interpreted text role "ref".The Stats Collector API is always available, so you can always use it (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 almost unnoticeable when disabled.
Common Stats Collector uses
Access the stats collector through the :attr:`~scrapy.crawler.Crawler.stats` attribute. Here is an example of an extension that accesses stats:
System Message: ERROR/3 (<stdin>, line 30); backlink
Unknown interpreted text role "attr".System Message: WARNING/2 (<stdin>, line 33)
Cannot analyze code. Pygments package not found.
.. code-block:: python
class ExtensionThatAccessStats:
def __init__(self, stats):
self.stats = stats
@classmethod
def from_crawler(cls, crawler):
return cls(crawler.stats)
Set stat value:
System Message: WARNING/2 (<stdin>, line 47)
Cannot analyze code. Pygments package not found.
.. code-block:: python
stats.set_value("hostname", socket.gethostname())
Increment stat value:
System Message: WARNING/2 (<stdin>, line 53)
Cannot analyze code. Pygments package not found.
.. code-block:: python
stats.inc_value("custom_count")
Set stat value only if greater than previous:
System Message: WARNING/2 (<stdin>, line 59)
Cannot analyze code. Pygments package not found.
.. code-block:: python
stats.max_value("max_items_scraped", value)
Set stat value only if lower than previous:
System Message: WARNING/2 (<stdin>, line 65)
Cannot analyze code. Pygments package not found.
.. code-block:: python
stats.min_value("min_free_memory_percent", value)
Get stat value:
System Message: WARNING/2 (<stdin>, line 71)
Cannot analyze code. Pygments package not found.
.. code-block:: pycon
>>> stats.get_value("custom_count")
1
Get all stats:
System Message: WARNING/2 (<stdin>, line 78)
Cannot analyze code. Pygments package not found.
.. code-block:: pycon
>>> stats.get_stats()
{'custom_count': 1, 'start_time': datetime.datetime(2009, 7, 14, 21, 47, 28, 977139)}
Available Stats Collectors
System Message: ERROR/3 (<stdin>, line 88)
Unknown directive type "currentmodule".
.. currentmodule:: scrapy.statscollectors
Besides the basic :class:`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 :setting:`STATS_CLASS` setting. The default Stats Collector used is the :class:`MemoryStatsCollector`.
System Message: ERROR/3 (<stdin>, line 90); backlink
Unknown interpreted text role "class".System Message: ERROR/3 (<stdin>, line 90); backlink
Unknown interpreted text role "setting".System Message: ERROR/3 (<stdin>, line 90); backlink
Unknown interpreted text role "class".MemoryStatsCollector
System Message: ERROR/3 (<stdin>, line 98)
Unknown directive type "autoclass".
.. autoclass:: MemoryStatsCollector :members:
DummyStatsCollector
System Message: ERROR/3 (<stdin>, line 104)
Unknown directive type "autoclass".
.. autoclass:: DummyStatsCollector