Scrapy 简明教程
Scrapy - Exceptions
Description
不规则的事件称为异常。在 Scrapy 中,异常是由配置缺失、从项目管道删除项目等原因引发的。以下是 Scrapy 中提到的异常及其应用程序列表。
The irregular events are referred to as exceptions. In Scrapy, exceptions are raised due to reasons such as missing configuration, dropping item from the item pipeline, etc. Following is the list of exceptions mentioned in Scrapy and their application.
DropItem
项目管道利用此异常在任何阶段停止处理项目。它可以写成−
Item Pipeline utilizes this exception to stop processing of the item at any stage. It can be written as −
exception (scrapy.exceptions.DropItem)
CloseSpider
此异常用于停止使用回调请求的蜘蛛。它可以写成−
This exception is used to stop the spider using the callback request. It can be written as −
exception (scrapy.exceptions.CloseSpider)(reason = 'cancelled')
它包含一个名为原因 (str) 的参数,该参数指定关闭的原因。
It contains parameter called reason (str) which specifies the reason for closing.
例如,以下代码显示了此异常用法−
For instance, the following code shows this exception usage −
def parse_page(self, response):
if 'Bandwidth exceeded' in response.body:
raise CloseSpider('bandwidth_exceeded')
IgnoreRequest
此异常由调度程序或下载器中间件用于忽略请求。它可以写成−
This exception is used by scheduler or downloader middleware to ignore a request. It can be written as −
exception (scrapy.exceptions.IgnoreRequest)
NotConfigured
它表示缺少配置的情况,并且应该在组件构造函数中引发。
It indicates a missing configuration situation and should be raised in a component constructor.
exception (scrapy.exceptions.NotConfigured)
如果以下任何组件停用,都可能会引发此异常。
This exception can be raised, if any of the following components are disabled.
-
Extensions
-
Item pipelines
-
Downloader middlewares
-
Spider middlewares