From a3f22046efaf661bd7d463decb65eea871b7a1d5 Mon Sep 17 00:00:00 2001 From: Andrey Rakhmatullin Date: Tue, 12 Sep 2023 20:41:36 +0400 Subject: [PATCH] Document changing settings in Spider.from_crawler(). --- docs/topics/settings.rst | 20 ++++++++++++++++++++ docs/topics/spiders.rst | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/docs/topics/settings.rst b/docs/topics/settings.rst index e1936eb5b..65823e071 100644 --- a/docs/topics/settings.rst +++ b/docs/topics/settings.rst @@ -98,6 +98,26 @@ and settings set there should use the "spider" priority explicitly: super().update_settings(settings) settings.set("SOME_SETTING", "some value", priority="spider") +.. versionadded:: VERSION + +It's also possible to modify the settings in the +:meth:`~scrapy.Spider.from_crawler` method, e.g. based on :ref:`spider +arguments ` or other logic: + +.. code-block:: python + + import scrapy + + + class MySpider(scrapy.Spider): + name = "myspider" + + @classmethod + def from_crawler(cls, crawler, *args, **kwargs): + spider = super().from_crawler(crawler, *args, **kwargs) + spider.settings.set("SOME_SETTING", kwargs["value"], priority="spider") + return spider + 3. Project settings module -------------------------- diff --git a/docs/topics/spiders.rst b/docs/topics/spiders.rst index 5c3bf6e72..4ed9b8dc3 100644 --- a/docs/topics/spiders.rst +++ b/docs/topics/spiders.rst @@ -136,6 +136,14 @@ scrapy.Spider attributes in the new instance so they can be accessed later inside the spider's code. + .. versionchanged:: VERSION + + The settings available in this method can now be modified, which is + handy if you want to modify them based on arguments. As a + consequence, the settings available in this method aren't the final + values as they can be modified later by e.g. :ref:`add-ons + `. + :param crawler: crawler to which the spider will be bound :type crawler: :class:`~scrapy.crawler.Crawler` instance