scrapy/docs/topics/exceptions.rst

3.3 KiB

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

Exceptions

Built-in Exceptions reference

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

Unknown directive type "module".

.. module:: scrapy.exceptions
   :synopsis: Scrapy exceptions

Here's a list of all exceptions included in Scrapy and their usage, except for the :ref:`download handler exceptions <download-handlers-exceptions>`.

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

Unknown interpreted text role "ref".

CloseSpider

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

Unknown directive type "exception".

.. exception:: CloseSpider(reason='cancelled')

    This exception can be raised from a spider callback to request the spider to be
    closed/stopped. Supported arguments:

    :param reason: the reason for closing
    :type reason: str

For example:

System Message: WARNING/2 (<stdin>, line 32)

Cannot analyze code. Pygments package not found.

.. code-block:: python

    def parse_page(self, response):
        if "Bandwidth exceeded" in response.text:
            raise CloseSpider("bandwidth_exceeded")

DontCloseSpider

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

Unknown directive type "exception".

.. exception:: DontCloseSpider

This exception can be raised in a :signal:`spider_idle` signal handler to prevent the spider from being closed.

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

Unknown interpreted text role "signal".

DropItem

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

Unknown directive type "exception".

.. exception:: DropItem

The exception that must be raised by item pipeline stages to stop processing an Item. For more information see :ref:`topics-item-pipeline`.

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

Unknown interpreted text role "ref".

IgnoreRequest

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

Unknown directive type "exception".

.. exception:: IgnoreRequest

This exception can be raised by the Scheduler or any downloader middleware to indicate that the request should be ignored.

NotConfigured

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

Unknown directive type "exception".

.. exception:: NotConfigured

This exception can be raised by some components to indicate that they will remain disabled. Those components include:

  • Extensions
  • Item pipelines
  • Downloader middlewares
  • Spider middlewares

The exception must be raised in the component's __init__() or from_crawler() method.

NotSupported

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

Unknown directive type "exception".

.. exception:: NotSupported

This exception is raised to indicate an unsupported feature.

StopDownload

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

Unknown directive type "exception".

.. exception:: StopDownload(fail=True)

Raised from a :class:`~scrapy.signals.bytes_received` or :class:`~scrapy.signals.headers_received` signal handler to indicate that no further bytes should be downloaded for a response.

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

Unknown interpreted text role "class".

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

Unknown interpreted text role "class".

The fail boolean parameter controls which method will handle the resulting response:

  • If fail=True (default), the request errback is called. The response object is available as the response attribute of the StopDownload exception, which is in turn stored as the value attribute of the received :class:`~twisted.python.failure.Failure` object. This means that in an errback defined as def errback(self, failure), the response can be accessed though failure.value.response.

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

    Unknown interpreted text role "class".

  • If fail=False, the request callback is called instead.

In both cases, the response could have its body truncated: the body contains all bytes received up until the exception is raised, including the bytes received in the signal handler that raises the exception. Also, the response object is marked with "download_stopped" in its :attr:`~scrapy.http.Response.flags` attribute.

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

Unknown interpreted text role "attr".

Note

fail is a keyword-only parameter, i.e. raising StopDownload(False) or StopDownload(True) will raise a :class:`TypeError`.

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

Unknown interpreted text role "class".

See the documentation for the :class:`~scrapy.signals.bytes_received` and :class:`~scrapy.signals.headers_received` signals and the :ref:`topics-stop-response-download` topic for additional information and examples.

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

Unknown interpreted text role "class".

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

Unknown interpreted text role "class".

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

Unknown interpreted text role "ref".
</html>