Returns a new :class:`~scrapy.Request` object to retry the specified request, or ``None`` if retries of the specified request have been exhausted. For example, in a :class:`~scrapy.Spider` callback, you could use it as follows:: def parse(self, response): i
(
request: Request,
*,
spider: scrapy.Spider,
reason: str | Exception | type[Exception] = "unspecified",
max_retry_times: int | None = None,
priority_adjust: int | None = None,
logger: Logger = retry_logger,
stats_base_key: str = "retry",
)
| 36 | |
| 37 | |
| 38 | def get_retry_request( |
| 39 | request: Request, |
| 40 | *, |
| 41 | spider: scrapy.Spider, |
| 42 | reason: str | Exception | type[Exception] = "unspecified", |
| 43 | max_retry_times: int | None = None, |
| 44 | priority_adjust: int | None = None, |
| 45 | logger: Logger = retry_logger, |
| 46 | stats_base_key: str = "retry", |
| 47 | ) -> Request | None: |
| 48 | """ |
| 49 | Returns a new :class:`~scrapy.Request` object to retry the specified |
| 50 | request, or ``None`` if retries of the specified request have been |
| 51 | exhausted. |
| 52 | |
| 53 | For example, in a :class:`~scrapy.Spider` callback, you could use it as |
| 54 | follows:: |
| 55 | |
| 56 | def parse(self, response): |
| 57 | if not response.text: |
| 58 | new_request_or_none = get_retry_request( |
| 59 | response.request, |
| 60 | spider=self, |
| 61 | reason='empty', |
| 62 | ) |
| 63 | return new_request_or_none |
| 64 | |
| 65 | *spider* is the :class:`~scrapy.Spider` instance which is asking for the |
| 66 | retry request. It is used to access the :ref:`settings <topics-settings>` |
| 67 | and :ref:`stats <topics-stats>`, and to provide extra logging context (see |
| 68 | :func:`logging.debug`). |
| 69 | |
| 70 | *reason* is a string or an :class:`Exception` object that indicates the |
| 71 | reason why the request needs to be retried. It is used to name retry stats. |
| 72 | |
| 73 | *max_retry_times* is a number that determines the maximum number of times |
| 74 | that *request* can be retried. If not specified or ``None``, the number is |
| 75 | read from the :reqmeta:`max_retry_times` meta key of the request. If the |
| 76 | :reqmeta:`max_retry_times` meta key is not defined or ``None``, the number |
| 77 | is read from the :setting:`RETRY_TIMES` setting. |
| 78 | |
| 79 | *priority_adjust* is a number that determines how the priority of the new |
| 80 | request changes in relation to *request*. If not specified, the number is |
| 81 | read from the :setting:`RETRY_PRIORITY_ADJUST` setting. |
| 82 | |
| 83 | *logger* is the logging.Logger object to be used when logging messages |
| 84 | |
| 85 | *stats_base_key* is a string to be used as the base key for the |
| 86 | retry-related job stats |
| 87 | """ |
| 88 | settings = spider.crawler.settings |
| 89 | assert spider.crawler.stats |
| 90 | stats = spider.crawler.stats |
| 91 | retry_times = request.meta.get("retry_times", 0) + 1 |
| 92 | if max_retry_times is None: |
| 93 | max_retry_times = request.meta.get("max_retry_times") |
| 94 | if max_retry_times is None: |
| 95 | max_retry_times = settings.getint("RETRY_TIMES") |