<a id="topics-exceptions"></a>

<a id="topics-exceptions-ref"></a>

# Exceptions

Here’s a list of all exceptions included in Scrapy and their usage, except for
the [download handler exceptions](download-handlers.md#download-handlers-exceptions).

<a id="module-scrapy.exceptions"></a>

### *exception* scrapy.exceptions.CloseSpider(reason: [str](https://docs.python.org/3/library/stdtypes.html#str) = 'cancelled')

Raised from a [spider callback](spiders.md#topics-spiders), or while the
spider is starting, to request the spider to be closed/stopped.

> [!WARNING]
> Changed in version 2.18.0: Added support for raising it while the spider is starting.

*reason* is a string with the reason for closing.

For example:

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

### *exception* scrapy.exceptions.DontCloseSpider

Raised in a [`spider_idle`](signals.md#std-signal-spider_idle) signal handler to prevent the spider
from being closed.

### *exception* scrapy.exceptions.DropItem(message: [str](https://docs.python.org/3/library/stdtypes.html#str), log_level: [str](https://docs.python.org/3/library/stdtypes.html#str) | [None](https://docs.python.org/3/library/constants.html#None) = None)

Raised from the [`process_item()`](item-pipeline.md#process_item) method of an [item pipeline](item-pipeline.md#topics-item-pipeline) to stop the processing of an item.

### *exception* scrapy.exceptions.IgnoreRequest

Raised to indicate that a request should be ignored.

A [downloader middleware](downloader-middleware.md#topics-downloader-middleware) can raise it
from its
[`process_request()`](downloader-middleware.md#scrapy.downloadermiddlewares.DownloaderMiddleware.process_request)
or
[`process_response()`](downloader-middleware.md#scrapy.downloadermiddlewares.DownloaderMiddleware.process_response)
method to drop a request, and a [`request_scheduled`](signals.md#std-signal-request_scheduled) signal handler
can raise it to drop a request before it reaches the
[scheduler](scheduler.md#topics-scheduler).

### *exception* scrapy.exceptions.NotConfigured

Raised by a [component](components.md#topics-components) from its `__init__()`
or [`from_crawler()`](components.md#from_crawler) method to indicate that it will remain disabled.

Only the following components can be disabled this way:

- [Downloader middlewares](downloader-middleware.md#topics-downloader-middleware)
- [Extensions](extensions.md#topics-extensions)
- [Item pipelines](item-pipeline.md#topics-item-pipeline)
- [Spider middlewares](spider-middleware.md#topics-spider-middleware)

### *exception* scrapy.exceptions.NotSupported

Raised to indicate that a requested feature is not supported.

For example, Scrapy raises it when text-parsing shortcuts such as
[`response.css()`](request-response.md#scrapy.http.TextResponse.css) or
[`response.xpath()`](request-response.md#scrapy.http.TextResponse.xpath) are used on a
[`Response`](request-response.md#scrapy.http.Response) whose content is not text, or when sending a
request whose URL scheme has no matching [download handler](download-handlers.md#topics-download-handlers).

### *exception* scrapy.exceptions.StopDownload(\*, fail: [bool](https://docs.python.org/3/library/functions.html#bool) = True)

Raised from a [`bytes_received`](signals.md#scrapy.signals.bytes_received) or
[`headers_received`](signals.md#scrapy.signals.headers_received) signal handler to [stop the
download](request-response.md#topics-stop-response-download) of the response body.

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 [`Failure`](https://docs.twisted.org/en/stable/api/twisted.python.failure.Failure.html) object. This means that
  in an errback defined as `def errback(self, failure)`, the response can
  be accessed though `failure.value.response`.
* 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
[`flags`](request-response.md#scrapy.http.Response.flags) attribute.
