7.5 KiB
Core API
This section documents the Scrapy core API, and it's intended for developers of extensions and middlewares.
Crawler API
The main entry point to the Scrapy API is the :class:`~scrapy.crawler.Crawler` object, which :ref:`components <topics-components>` can :ref:`get for initialization <from-crawler>`. It provides access to all Scrapy core components, and it is the only way for components to access them and hook their functionality into Scrapy.
System Message: ERROR/3 (<stdin>, line 15); backlink
Unknown interpreted text role "class".System Message: ERROR/3 (<stdin>, line 15); backlink
Unknown interpreted text role "ref".System Message: ERROR/3 (<stdin>, line 15); backlink
Unknown interpreted text role "ref".System Message: ERROR/3 (<stdin>, line 21)
Unknown directive type "module".
.. module:: scrapy.crawler :synopsis: The Scrapy crawler
The Extension Manager is responsible for loading and keeping track of installed extensions and it's configured through the :setting:`EXTENSIONS` setting which contains a dictionary of all available extensions and their order similar to how you :ref:`configure the downloader middlewares <topics-downloader-middleware-setting>`.
System Message: ERROR/3 (<stdin>, line 24); backlink
Unknown interpreted text role "setting".System Message: ERROR/3 (<stdin>, line 24); backlink
Unknown interpreted text role "ref".System Message: ERROR/3 (<stdin>, line 30)
Unknown directive type "autoclass".
.. autoclass:: Crawler
:members: get_addon, get_downloader_middleware, get_extension,
get_item_pipeline, get_spider_middleware
The Crawler object must be instantiated with a
:class:`scrapy.Spider` subclass and a
:class:`scrapy.settings.Settings` object.
The :attr:`engine`, :attr:`extensions`, :attr:`logformatter`,
:attr:`request_fingerprinter` and :attr:`stats` attributes get their value
when the crawl starts, and raise :exc:`RuntimeError` when read before that.
.. versionchanged:: VERSION
Those attributes used to be ``None`` before getting their value.
.. attribute:: request_fingerprinter
The request fingerprint builder of this crawler.
This is used from extensions and middlewares to build short, unique
identifiers for requests. See :ref:`request-fingerprints`.
.. attribute:: settings
The settings manager of this crawler.
This is used by extensions & middlewares to access the Scrapy settings
of this crawler.
For an introduction on Scrapy settings see :ref:`topics-settings`.
For the API see :class:`~scrapy.settings.Settings` class.
.. attribute:: signals
The signals manager of this crawler.
This is used by extensions & middlewares to hook themselves into Scrapy
functionality.
For an introduction on signals see :ref:`topics-signals`.
For the API see :class:`~scrapy.signalmanager.SignalManager` class.
.. attribute:: stats
The stats collector of this crawler.
This is used from extensions & middlewares to record stats of their
behaviour, or access stats collected by other extensions.
For an introduction on stats collection see :ref:`topics-stats`.
For the API see :class:`~scrapy.statscollectors.StatsCollector` class.
.. attribute:: extensions
The extension manager that keeps track of enabled extensions.
Most extensions won't need to access this attribute.
For an introduction on extensions and a list of available extensions on
Scrapy see :ref:`topics-extensions`.
.. attribute:: engine
The execution engine, which coordinates the core crawling logic
between the scheduler, downloader and spiders.
Some extension may want to access the Scrapy engine, to inspect or
modify the downloader and scheduler behaviour, although this is an
advanced use and this API is not yet stable.
.. attribute:: spider
Spider currently being crawled. This is an instance of the spider class
provided while constructing the crawler, and it is created after the
arguments given in the :meth:`crawl` method.
.. automethod:: crawl_async
.. automethod:: crawl
.. automethod:: stop_async
.. automethod:: stop
System Message: ERROR/3 (<stdin>, line 117)
Unknown directive type "autoclass".
.. autoclass:: AsyncCrawlerRunner :members:
System Message: ERROR/3 (<stdin>, line 120)
Unknown directive type "autoclass".
.. autoclass:: CrawlerRunner :members:
System Message: ERROR/3 (<stdin>, line 123)
Unknown directive type "autoclass".
.. autoclass:: AsyncCrawlerProcess :show-inheritance: :members: :inherited-members:
System Message: ERROR/3 (<stdin>, line 128)
Unknown directive type "autoclass".
.. autoclass:: CrawlerProcess :show-inheritance: :members: :inherited-members:
Settings API
System Message: ERROR/3 (<stdin>, line 138)
Unknown directive type "module".
.. module:: scrapy.settings :synopsis: Settings manager
System Message: ERROR/3 (<stdin>, line 141)
Unknown directive type "attribute".
.. attribute:: SETTINGS_PRIORITIES
Dictionary that sets the key name and priority level of the default
settings priorities used in Scrapy.
Each item defines a settings entry point, giving it a code name for
identification and an integer priority. Greater priorities take more
precedence over lesser ones when setting and retrieving values in the
:class:`~scrapy.settings.Settings` class.
.. code-block:: python
SETTINGS_PRIORITIES = {
"default": 0,
"command": 10,
"addon": 15,
"project": 20,
"spider": 30,
"cmdline": 40,
}
For a detailed explanation on each settings sources, see:
:ref:`topics-settings`.
System Message: ERROR/3 (<stdin>, line 165)
Unknown directive type "autofunction".
.. autofunction:: get_settings_priority
System Message: ERROR/3 (<stdin>, line 167)
Unknown directive type "autoclass".
.. autoclass:: Settings :show-inheritance: :members:
System Message: ERROR/3 (<stdin>, line 171)
Unknown directive type "autoclass".
.. autoclass:: BaseSettings :members:
SpiderLoader API
System Message: ERROR/3 (<stdin>, line 179)
Unknown directive type "module".
.. module:: scrapy.spiderloader :synopsis: The spider loader
Custom spider loaders can be employed by specifying their path in the :setting:`SPIDER_LOADER_CLASS` project setting. They must implement :class:`SpiderLoaderProtocol`.
System Message: ERROR/3 (<stdin>, line 182); backlink
Unknown interpreted text role "setting".System Message: ERROR/3 (<stdin>, line 182); backlink
Unknown interpreted text role "class".System Message: ERROR/3 (<stdin>, line 186)
Unknown directive type "autoclass".
.. autoclass:: SpiderLoaderProtocol
:members:
System Message: ERROR/3 (<stdin>, line 189)
Unknown directive type "autoclass".
.. autoclass:: SpiderLoader
:members:
System Message: ERROR/3 (<stdin>, line 192)
Unknown directive type "autoclass".
.. autoclass:: DummySpiderLoader
Signals API
System Message: ERROR/3 (<stdin>, line 199)
Unknown directive type "automodule".
.. automodule:: scrapy.signalmanager
:synopsis: The signal manager
:members:
:undoc-members:
Stats Collector API
There are several Stats Collectors available under the :mod:`scrapy.statscollectors` module and they all implement the Stats Collector API defined by the :class:`~scrapy.statscollectors.StatsCollector` class (which they all inherit from).
System Message: ERROR/3 (<stdin>, line 209); backlink
Unknown interpreted text role "mod".System Message: ERROR/3 (<stdin>, line 209); backlink
Unknown interpreted text role "class".System Message: ERROR/3 (<stdin>, line 214)
Unknown directive type "module".
.. module:: scrapy.statscollectors :synopsis: Stats Collectors
System Message: ERROR/3 (<stdin>, line 219)
Unknown directive type "method".
.. method:: get_value(key, default=None)
Return the value for the given stats key or default if it doesn't exist.
System Message: ERROR/3 (<stdin>, line 223)
Unknown directive type "method".
.. method:: get_stats()
Get all stats from the currently running spider as a dict.
System Message: ERROR/3 (<stdin>, line 227)
Unknown directive type "method".
.. method:: set_value(key, value)
Set the given value for the given stats key.
System Message: ERROR/3 (<stdin>, line 231)
Unknown directive type "method".
.. method:: set_stats(stats)
Override the current stats with the dict passed in ``stats`` argument.
System Message: ERROR/3 (<stdin>, line 235)
Unknown directive type "method".
.. method:: inc_value(key, count=1, start=0)
Increment the value of the given stats key, by the given count,
assuming the start value given (when it's not set).
System Message: ERROR/3 (<stdin>, line 240)
Unknown directive type "method".
.. method:: max_value(key, value)
Set the given value for the given key only if current value for the
same key is lower than value. If there is no current value for the
given key, the value is always set.
System Message: ERROR/3 (<stdin>, line 246)
Unknown directive type "method".
.. method:: min_value(key, value)
Set the given value for the given key only if current value for the
same key is greater than value. If there is no current value for the
given key, the value is always set.
System Message: ERROR/3 (<stdin>, line 252)
Unknown directive type "method".
.. method:: clear_stats()
Clear all stats.
The following methods are not part of the stats collection api but instead used when implementing custom stats collectors:
System Message: ERROR/3 (<stdin>, line 259)
Unknown directive type "method".
.. method:: open_spider()
Open the spider for stats collection.
System Message: ERROR/3 (<stdin>, line 263)
Unknown directive type "method".
.. method:: close_spider()
Close the spider. After this is called, no more specific stats
can be accessed or collected.
Engine API
System Message: ERROR/3 (<stdin>, line 271)
Unknown directive type "autoclass".
.. autoclass:: scrapy.core.engine.ExecutionEngine() :members: needs_backout