- Scrapy 教程
- Scrapy - 首頁
- Scrapy 基本概念
- Scrapy - 概述
- Scrapy - 環境
- Scrapy - 命令列工具
- Scrapy - 爬蟲
- Scrapy - 選擇器
- Scrapy - 專案
- Scrapy - 專案載入器
- Scrapy - Shell
- Scrapy - 專案管道
- Scrapy - 資料匯出
- Scrapy - 請求 & 響應
- Scrapy - 連結提取器
- Scrapy - 設定
- Scrapy - 異常
- Scrapy 即時專案
- Scrapy - 建立專案
- Scrapy - 定義專案
- Scrapy - 第一個爬蟲
- Scrapy - 爬取
- Scrapy - 提取專案
- Scrapy - 使用專案
- Scrapy - 跟蹤連結
- Scrapy - 爬取資料
- Scrapy 有用資源
- Scrapy - 快速指南
- Scrapy - 有用資源
- Scrapy - 討論
Scrapy - 異常
描述
不規則事件被稱為異常。在 Scrapy 中,異常的發生原因包括配置缺失、從專案管道中丟棄專案等。以下是 Scrapy 中提到的異常及其應用列表。
DropItem
專案管道利用此異常在任何階段停止處理專案。可以寫成:
exception (scrapy.exceptions.DropItem)
CloseSpider
此異常用於使用回撥請求停止爬蟲。可以寫成:
exception (scrapy.exceptions.CloseSpider)(reason = 'cancelled')
它包含一個名為 reason (str) 的引數,用於指定關閉的原因。
例如,以下程式碼展示了此異常的使用:
def parse_page(self, response):
if 'Bandwidth exceeded' in response.body:
raise CloseSpider('bandwidth_exceeded')
IgnoreRequest
此異常由排程器或下載器中介軟體用於忽略請求。可以寫成:
exception (scrapy.exceptions.IgnoreRequest)
NotConfigured
它表示配置缺失的情況,應在元件建構函式中引發。
exception (scrapy.exceptions.NotConfigured)
如果以下任何元件被停用,則可以引發此異常。
- 擴充套件
- 專案管道
- 下載器中介軟體
- 爬蟲中介軟體
NotSupported
當任何功能或方法不受支援時,會引發此異常。可以寫成:
exception (scrapy.exceptions.NotSupported)
廣告