Exceptions
Here’s a list of all exceptions included in Scrapy and their usage, except for the download handler exceptions.
- exception scrapy.exceptions.CloseSpider(reason: str = 'cancelled')[source]
Raised from a spider callback to request the spider to be closed/stopped.
reason is a string with the reason for closing.
For example:
def parse_page(self, response): if "Bandwidth exceeded" in response.text: raise CloseSpider("bandwidth_exceeded")
- exception scrapy.exceptions.DontCloseSpider[source]
Raised in a
spider_idlesignal handler to prevent the spider from being closed.
- exception scrapy.exceptions.DropItem(message: str, log_level: str | None = None)[source]
Raised from the
process_item()method of an item pipeline to stop the processing of an item.
- exception scrapy.exceptions.IgnoreRequest[source]
Raised to indicate that a request should be ignored.
A downloader middleware can raise it from its
process_request()orprocess_response()method to drop a request, and arequest_scheduledsignal handler can raise it to drop a request before it reaches the scheduler.
- exception scrapy.exceptions.NotConfigured[source]
Raised by a component from its
__init__()orfrom_crawler()method to indicate that it will remain disabled.Only the following components can be disabled this way:
- exception scrapy.exceptions.NotSupported[source]
Raised to indicate that a requested feature is not supported.
For example, Scrapy raises it when text-parsing shortcuts such as
response.css()orresponse.xpath()are used on aResponsewhose content is not text, or when sending a request whose URL scheme has no matching download handler.
- exception scrapy.exceptions.StopDownload(*, fail: bool = True)[source]
Raised from a
bytes_receivedorheaders_receivedsignal handler to stop the download of the response body.The
failboolean parameter controls which method will handle the resulting response:If
fail=True(default), the request errback is called. The response object is available as theresponseattribute of theStopDownloadexception, which is in turn stored as thevalueattribute of the receivedFailureobject. This means that in an errback defined asdef errback(self, failure), the response can be accessed thoughfailure.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 itsflagsattribute.