scrapy/docs/topics/api.rst

15 KiB

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> </head>

Core API

System Message: ERROR/3 (<stdin>, line 7)

Unknown directive type "versionadded".

.. versionadded:: 0.15

This section documents the Scrapy core API, and it's intended for developers of extensions and middlewares.

Crawler API

The main entry point to Scrapy API is the :class:`~scrapy.crawler.Crawler` object, passed to extensions through the from_crawler class method. This object provides access to all Scrapy core components, and it's the only way for extensions to access them and hook their functionality into Scrapy.

System Message: ERROR/3 (<stdin>, line 17); backlink

Unknown interpreted text role "class".

System Message: ERROR/3 (<stdin>, line 22)

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 25); backlink

Unknown interpreted text role "setting".

System Message: ERROR/3 (<stdin>, line 25); backlink

Unknown interpreted text role "ref".

The Crawler object must be instantiated with a :class:`scrapy.spiders.Spider` subclass and a :class:`scrapy.settings.Settings` object.

System Message: ERROR/3 (<stdin>, line 33); backlink

Unknown interpreted text role "class".

System Message: ERROR/3 (<stdin>, line 33); backlink

Unknown interpreted text role "class".

System Message: ERROR/3 (<stdin>, line 37)

Unknown directive type "attribute".

.. 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.

System Message: ERROR/3 (<stdin>, line 48)

Unknown directive type "attribute".

.. 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.

System Message: ERROR/3 (<stdin>, line 59)

Unknown directive type "attribute".

.. 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.

System Message: ERROR/3 (<stdin>, line 70)

Unknown directive type "attribute".

.. 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`.

System Message: ERROR/3 (<stdin>, line 79)

Unknown directive type "attribute".

.. 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.

System Message: ERROR/3 (<stdin>, line 88)

Unknown directive type "attribute".

.. 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.

System Message: ERROR/3 (<stdin>, line 94)

Unknown directive type "method".

.. method:: crawl(\*args, \**kwargs)

    Starts the crawler by instantiating its spider class with the given
    `args` and `kwargs` arguments, while setting the execution engine in
    motion.

    Returns a deferred that is fired when the crawl is finished.

System Message: ERROR/3 (<stdin>, line 102)

Unknown directive type "autoclass".

.. autoclass:: CrawlerRunner
   :members:

System Message: ERROR/3 (<stdin>, line 105)

Unknown directive type "autoclass".

.. autoclass:: CrawlerProcess
   :show-inheritance:
   :members:
   :inherited-members:

Settings API

System Message: ERROR/3 (<stdin>, line 115)

Unknown directive type "module".

.. module:: scrapy.settings
   :synopsis: Settings manager

System Message: ERROR/3 (<stdin>, line 118)

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.

    .. highlight:: python

    ::

        SETTINGS_PRIORITIES = {
            'default': 0,
            'command': 10,
            'project': 20,
            'spider': 30,
            'cmdline': 40,
        }

    For a detailed explanation on each settings sources, see:
    :ref:`topics-settings`.

System Message: ERROR/3 (<stdin>, line 143)

Unknown directive type "function".

.. function:: get_settings_priority(priority)

    Small helper function that looks up a given string priority in the
    :attr:`~scrapy.settings.SETTINGS_PRIORITIES` dictionary and returns its
    numerical value, or directly returns a given numerical priority.

This object stores Scrapy settings for the configuration of internal components, and can be used for any further customization.

It is a direct subclass and supports all methods of :class:`~scrapy.settings.BaseSettings`. Additionally, after instantiation of this class, the new object will have the global default settings described on :ref:`topics-settings-ref` already populated.

System Message: ERROR/3 (<stdin>, line 154); backlink

Unknown interpreted text role "class".

System Message: ERROR/3 (<stdin>, line 154); backlink

Unknown interpreted text role "ref".

Instances of this class behave like dictionaries, but store priorities along with their (key, value) pairs, and can be frozen (i.e. marked immutable).

Key-value entries can be passed on initialization with the values argument, and they would take the priority level (unless values is already an instance of :class:`~scrapy.settings.BaseSettings`, in which case the existing priority levels will be kept). If the priority argument is a string, the priority name will be looked up in :attr:`~scrapy.settings.SETTINGS_PRIORITIES`. Otherwise, a specific integer should be provided.

System Message: ERROR/3 (<stdin>, line 165); backlink

Unknown interpreted text role "class".

System Message: ERROR/3 (<stdin>, line 165); backlink

Unknown interpreted text role "attr".

Once the object is created, new settings can be loaded or updated with the :meth:`~scrapy.settings.BaseSettings.set` method, and can be accessed with the square bracket notation of dictionaries, or with the :meth:`~scrapy.settings.BaseSettings.get` method of the instance and its value conversion variants. When requesting a stored key, the value with the highest priority will be retrieved.

System Message: ERROR/3 (<stdin>, line 173); backlink

Unknown interpreted text role "meth".

System Message: ERROR/3 (<stdin>, line 173); backlink

Unknown interpreted text role "meth".

System Message: ERROR/3 (<stdin>, line 180)

Unknown directive type "method".

.. method:: set(name, value, priority='project')

   Store a key/value attribute with a given priority.

   Settings should be populated *before* configuring the Crawler object
   (through the :meth:`~scrapy.crawler.Crawler.configure` method),
   otherwise they won't have any effect.

   :param name: the setting name
   :type name: string

   :param value: the value to associate with the setting
   :type value: any

   :param priority: the priority of the setting. Should be a key of
       :attr:`~scrapy.settings.SETTINGS_PRIORITIES` or an integer
   :type priority: string or int

System Message: ERROR/3 (<stdin>, line 198)

Unknown directive type "method".

.. method:: update(values, priority='project')

   Store key/value pairs with a given priority.

   This is a helper function that calls
   :meth:`~scrapy.settings.BaseSettings.set` for every item of ``values``
   with the provided ``priority``.

   If ``values`` is a string, it is assumed to be JSON-encoded and parsed
   into a dict with ``json.loads()`` first. If it is a
   :class:`~scrapy.settings.BaseSettings` instance, the per-key priorities
   will be used and the ``priority`` parameter ignored. This allows
   inserting/updating settings with different priorities with a single
   command.

   :param values: the settings names and values
   :type values: dict or string or :class:`~scrapy.settings.BaseSettings`

   :param priority: the priority of the settings. Should be a key of
       :attr:`~scrapy.settings.SETTINGS_PRIORITIES` or an integer
   :type priority: string or int

System Message: ERROR/3 (<stdin>, line 220)

Unknown directive type "method".

.. method:: setmodule(module, priority='project')

   Store settings from a module with a given priority.

   This is a helper function that calls
   :meth:`~scrapy.settings.BaseSettings.set` for every globally declared
   uppercase variable of ``module`` with the provided ``priority``.

   :param module: the module or the path of the module
   :type module: module object or string

   :param priority: the priority of the settings. Should be a key of
       :attr:`~scrapy.settings.SETTINGS_PRIORITIES` or an integer
   :type priority: string or int

System Message: ERROR/3 (<stdin>, line 235)

Unknown directive type "method".

.. method:: get(name, default=None)

   Get a setting value without affecting its original type.

   :param name: the setting name
   :type name: string

   :param default: the value to return if no setting is found
   :type default: any

System Message: ERROR/3 (<stdin>, line 245)

Unknown directive type "method".

.. method:: getbool(name, default=False)

   Get a setting value as a boolean. For example, both ``1`` and ``'1'``, and
   ``True`` return ``True``, while ``0``, ``'0'``, ``False`` and ``None``
   return ``False````

   For example, settings populated through environment variables set to ``'0'``
   will return ``False`` when using this method.

   :param name: the setting name
   :type name: string

   :param default: the value to return if no setting is found
   :type default: any

System Message: ERROR/3 (<stdin>, line 260)

Unknown directive type "method".

.. method:: getint(name, default=0)

   Get a setting value as an int

   :param name: the setting name
   :type name: string

   :param default: the value to return if no setting is found
   :type default: any

System Message: ERROR/3 (<stdin>, line 270)

Unknown directive type "method".

.. method:: getfloat(name, default=0.0)

   Get a setting value as a float

   :param name: the setting name
   :type name: string

   :param default: the value to return if no setting is found
   :type default: any

System Message: ERROR/3 (<stdin>, line 280)

Unknown directive type "method".

.. method:: getlist(name, default=None)

   Get a setting value as a list. If the setting original type is a list, a
   copy of it will be returned. If it's a string it will be split by ",".

   For example, settings populated through environment variables set to
   ``'one,two'`` will return a list ['one', 'two'] when using this method.

   :param name: the setting name
   :type name: string

   :param default: the value to return if no setting is found
   :type default: any

System Message: ERROR/3 (<stdin>, line 294)

Unknown directive type "method".

.. method:: getdict(name, default=None)

   Get a setting value as a dictionary. If the setting original type is a
   dictionary, a copy of it will be returned. If it is a string it will be
   evaluated as a JSON dictionary. In the case that it is a
   :class:`~scrapy.settings.BaseSettings` instance itself, it will be
   converted to a dictionary, containing all its current settings values
   as they would be returned by :meth:`~scrapy.settings.BaseSettings.get`,
   and losing all information about priority and mutability.

   :param name: the setting name
   :type name: string

   :param default: the value to return if no setting is found
   :type default: any

System Message: ERROR/3 (<stdin>, line 310)

Unknown directive type "method".

.. method:: copy()

   Make a deep copy of current settings.

   This method returns a new instance of the :class:`Settings` class,
   populated with the same values and their priorities.

   Modifications to the new object won't be reflected on the original
   settings.

System Message: ERROR/3 (<stdin>, line 320)

Unknown directive type "method".

.. method:: freeze()

   Disable further changes to the current settings.

   After calling this method, the present state of the settings will become
   immutable. Trying to change values through the :meth:`~set` method and
   its variants won't be possible and will be alerted.

System Message: ERROR/3 (<stdin>, line 328)

Unknown directive type "method".

.. method:: frozencopy()

   Return an immutable copy of the current settings.

   Alias for a :meth:`~freeze` call in the object returned by :meth:`copy`

System Message: ERROR/3 (<stdin>, line 334)

Unknown directive type "method".

.. method:: getpriority(name)

   Return the current numerical priority value of a setting, or ``None`` if
   the given ``name`` does not exist.

   :param name: the setting name
   :type name: string

System Message: ERROR/3 (<stdin>, line 342)

Unknown directive type "method".

.. method:: maxpriority()

   Return the numerical value of the highest priority present throughout
   all settings, or the numerical value for ``default`` from
   :attr:`~scrapy.settings.SETTINGS_PRIORITIES` if there are no settings
   stored.

SpiderLoader API

System Message: ERROR/3 (<stdin>, line 354)

Unknown directive type "module".

.. module:: scrapy.loader
   :synopsis: The spider loader

This class is in charge of retrieving and handling the spider classes defined across the project.

Custom spider loaders can be employed by specifying their path in the :setting:`SPIDER_LOADER_CLASS` project setting. They must fully implement the :class:`scrapy.interfaces.ISpiderLoader` interface to guarantee an errorless execution.

System Message: ERROR/3 (<stdin>, line 362); backlink

Unknown interpreted text role "setting".

System Message: ERROR/3 (<stdin>, line 362); backlink

Unknown interpreted text role "class".

System Message: ERROR/3 (<stdin>, line 367)

Unknown directive type "method".

.. method:: from_settings(settings)

   This class method is used by Scrapy to create an instance of the class.
   It's called with the current project settings, and it loads the spiders
   found in the modules of the :setting:`SPIDER_MODULES` setting.

   :param settings: project settings
   :type settings: :class:`~scrapy.settings.Settings` instance

System Message: ERROR/3 (<stdin>, line 376)

Unknown directive type "method".

.. method:: load(spider_name)

   Get the Spider class with the given name. It'll look into the previously
   loaded spiders for a spider class with name `spider_name` and will raise
   a KeyError if not found.

   :param spider_name: spider class name
   :type spider_name: str

System Message: ERROR/3 (<stdin>, line 385)

Unknown directive type "method".

.. method:: list()

   Get the names of the available spiders in the project.

System Message: ERROR/3 (<stdin>, line 389)

Unknown directive type "method".

.. method:: find_by_request(request)

   List the spiders' names that can handle the given request. Will try to
   match the request's url against the domains of the spiders.

   :param request: queried request
   :type request: :class:`~scrapy.http.Request` instance

Signals API

System Message: ERROR/3 (<stdin>, line 402)

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 412); backlink

Unknown interpreted text role "mod".

System Message: ERROR/3 (<stdin>, line 412); backlink

Unknown interpreted text role "class".

System Message: ERROR/3 (<stdin>, line 417)

Unknown directive type "module".

.. module:: scrapy.statscollectors
   :synopsis: Stats Collectors

System Message: ERROR/3 (<stdin>, line 422)

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 426)

Unknown directive type "method".

.. method:: get_stats()

    Get all stats from the currently running spider as a dict.

System Message: ERROR/3 (<stdin>, line 430)

Unknown directive type "method".

.. method:: set_value(key, value)

    Set the given value for the given stats key.

System Message: ERROR/3 (<stdin>, line 434)

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 438)

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 443)

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 449)

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 455)

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 462)

Unknown directive type "method".

.. method:: open_spider(spider)

    Open the given spider for stats collection.

System Message: ERROR/3 (<stdin>, line 466)

Unknown directive type "method".

.. method:: close_spider(spider)

    Close the given spider. After this is called, no more specific stats
    can be accessed or collected.
</html>