Merge pull request #5846 from jxlil/docs/spiders

Added documentation for Spider.update_settings
This commit is contained in:
Andrey Rakhmatullin 2023-08-02 21:09:44 +04:00 committed by GitHub
commit 7c6aaedda2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 2 deletions

View File

@ -67,8 +67,8 @@ Example::
---------------------- ----------------------
Spiders (See the :ref:`topics-spiders` chapter for reference) can define their Spiders (See the :ref:`topics-spiders` chapter for reference) can define their
own settings that will take precedence and override the project ones. They can own settings that will take precedence and override the project ones. One way
do so by setting their :attr:`~scrapy.Spider.custom_settings` attribute: to do so is by setting their :attr:`~scrapy.Spider.custom_settings` attribute:
.. code-block:: python .. code-block:: python
@ -82,6 +82,22 @@ do so by setting their :attr:`~scrapy.Spider.custom_settings` attribute:
"SOME_SETTING": "some value", "SOME_SETTING": "some value",
} }
It's often better to implement :meth:`~scrapy.Spider.update_settings` instead,
and settings set there should use the "spider" priority explicitly:
.. code-block:: python
import scrapy
class MySpider(scrapy.Spider):
name = "myspider"
@classmethod
def update_settings(cls, settings):
super().update_settings(settings)
settings.set("SOME_SETTING", "some value", priority="spider")
3. Project settings module 3. Project settings module
-------------------------- --------------------------

View File

@ -145,6 +145,46 @@ scrapy.Spider
:param kwargs: keyword arguments passed to the :meth:`__init__` method :param kwargs: keyword arguments passed to the :meth:`__init__` method
:type kwargs: dict :type kwargs: dict
.. classmethod:: update_settings(settings)
The ``update_settings()`` method is used to modify the spider's settings
and is called during initialization of a spider instance.
It takes a :class:`~scrapy.settings.Settings` object as a parameter and
can add or update the spider's configuration values. This method is a
class method, meaning that it is called on the :class:`~scrapy.Spider`
class and allows all instances of the spider to share the same
configuration.
While per-spider settings can be set in
:attr:`~scrapy.Spider.custom_settings`, using ``update_settings()``
allows you to dynamically add, remove or change settings based on other
settings, spider attributes or other factors and use setting priorities
other than ``'spider'``. Also, it's easy to extend ``update_settings()``
in a subclass by overriding it, while doing the same with
:attr:`~scrapy.Spider.custom_settings` can be hard.
For example, suppose a spider needs to modify :setting:`FEEDS`:
.. code-block:: python
import scrapy
class MySpider(scrapy.Spider):
name = "myspider"
custom_feed = {
"/home/user/documents/items.json": {
"format": "json",
"indent": 4,
}
}
@classmethod
def update_settings(cls, settings):
super().update_settings(settings)
settings.setdefault("FEEDS", {}).update(cls.custom_feed)
.. method:: start_requests() .. method:: start_requests()
This method must return an iterable with the first Requests to crawl for This method must return an iterable with the first Requests to crawl for