新版功能。
該節(jié)文檔講述 Scrapy 核心 API,目標(biāo)用戶是開(kāi)發(fā) Scrapy 擴(kuò)展(extensions)和中間件(middlewares)的開(kāi)發(fā)人員。
Scrapy API 的主要入口是 Crawler 的實(shí)例對(duì)象,通過(guò)類方法 from_crawler 將它傳遞給擴(kuò)展(extensions)。該對(duì)象提供對(duì)所有 Scrapy 核心組件的訪問(wèn),也是擴(kuò)展訪問(wèn) Scrapy 核心組件和掛載功能到 Scrapy 的唯一途徑。
Extension Manager 負(fù)責(zé)加載和跟蹤已經(jīng)安裝的擴(kuò)展,它通過(guò) EXTENSIONS 配置,包含一個(gè)所有可用擴(kuò)展的字典, 字典的順序跟你在 configure the downloader middlewares 配置的順序一致。
Crawler 必須使用 scrapy.spider.Spider 子類及 scrapy.settings.Settings 的對(duì)象進(jìn)行實(shí)例化
crawler 的配置管理器。
擴(kuò)展(extensions)和中間件(middlewares)使用它用來(lái)訪問(wèn) Scrapy 的配置。
關(guān)于 Scrapy 配置的介紹參考這里 Settings。
API 參考 Settings。
crawler 的信號(hào)管理器。
擴(kuò)展和中間件使用它將自己的功能掛載到 Scrapy。
關(guān)于信號(hào)的介紹參考信號(hào)(Signals)。
API 參考 SignalManager。
crawler 的統(tǒng)計(jì)信息收集器。
擴(kuò)展和中間件使用它記錄操作的統(tǒng)計(jì)信息,或者訪問(wèn)由其他擴(kuò)展收集的統(tǒng)計(jì)信息。
關(guān)于統(tǒng)計(jì)信息收集器的介紹參考數(shù)據(jù)收集(Stats Collection)。
API 參考類 StatsCollector class。
擴(kuò)展管理器,跟蹤所有開(kāi)啟的擴(kuò)展。
大多數(shù)擴(kuò)展不需要訪問(wèn)該屬性。
關(guān)于擴(kuò)展和可用擴(kuò)展列表器的介紹參考擴(kuò)展(Extensions)。
執(zhí)行引擎,協(xié)調(diào) crawler 的核心邏輯,包括調(diào)度,下載和 spider。
某些擴(kuò)展可能需要訪問(wèn) Scrapy 的引擎屬性,以修改檢查(modify inspect)或修改下載器和調(diào)度器的行為, 這是該 API 的高級(jí)使用,但還不穩(wěn)定。
根據(jù)給定的 args , kwargs 的參數(shù)來(lái)初始化 spider 類,啟動(dòng)執(zhí)行引擎,啟動(dòng) crawler。
返回一個(gè)延遲 deferred 對(duì)象,當(dāng)爬取結(jié)束時(shí)觸發(fā)它。
獲取某項(xiàng)配置的值,且不修改其原有的值。
參數(shù):
return False 將某項(xiàng)配置的值以布爾值形式返回。比如,1 和'1',True 都返回True, 而 0,'0',False 和 None 返回 False。
比如,通過(guò)環(huán)境變量計(jì)算將某項(xiàng)配置設(shè)置為 '0',通過(guò)該方法獲取得到 False。
參數(shù):
將某項(xiàng)配置的值以整數(shù)形式返回
參數(shù):
將某項(xiàng)配置的值以浮點(diǎn)數(shù)形式返回
參數(shù):
將某項(xiàng)配置的值以列表形式返回。如果配置值本來(lái)就是 list 則將返回其拷貝。如果是字符串,則返回被 ”,” 分割后的列表。
比如,某項(xiàng)值通過(guò)環(huán)境變量的計(jì)算被設(shè)置為'one,two',該方法返回[‘one’, ‘two’]。
參數(shù):
Get a setting value as a dictionary. If the setting original type is a dictionary, a copy of it will be returned. If it’s a string it will evaluated as a json dictionary.
參數(shù):
Make a deep copy of current settings.
This method returns a new instance of the Settings class, populated with the same values and their priorities.
Modifications to the new object won’t be reflected on the original settings.
Disable further changes to the current settings.
After calling this method, the present state of the settings will become immutable. Trying to change values through the set() method and its variants won’t be possible and will be alerted.
Return an immutable copy of the current settings.
Alias for a freeze() call in the object returned by copy()
This class is in charge of retrieving and handling the spider classes defined across the project.
Custom spider managers can be employed by specifying their path in the SPIDER_MANAGER_CLASS project setting. They must fully implement the scrapy.interfaces.ISpiderManager interface to guarantee an errorless execution.
This class method is used by Scrapy to create an instance of the class. It’s called with the current project settings, and it loads the spiders found in the modules of the SPIDER_MODULES setting.
參數(shù):
settings (Settings instance) – project settings
Get the Spider class with the given name. It’ll look into the previously loaded spiders for a spider class with name spider_name and will raise a KeyError if not found.
參數(shù):
spider_name (str) – spider class name
list()
Get the names of the available spiders in the project.
List the spiders’ names that can handle the given request. Will try to match the request’s url against the domains of the spiders.
參數(shù):
request (Request instance) – queried request
鏈接一個(gè)接收器函數(shù)(receiver function) 到一個(gè)信號(hào)(signal)。
signal 可以是任何對(duì)象,雖然 Scrapy 提供了一些預(yù)先定義好的信號(hào), 參考文檔信號(hào)(Signals)。
參數(shù):
發(fā)送一個(gè)信號(hào),捕獲異常并記錄日志。
關(guān)鍵字參數(shù)會(huì)傳遞給信號(hào)處理者(signal handlers)(通過(guò)方法 connect()關(guān)聯(lián))。
跟 send_catch_log()相似但支持返回 deferreds 形式的信號(hào)處理器。
返回一個(gè) deferred,當(dāng)所有的信號(hào)處理器的延遲被觸發(fā)時(shí)調(diào)用。發(fā)送一個(gè)信號(hào),處理異常并記錄日志。
關(guān)鍵字參數(shù)會(huì)傳遞給信號(hào)處理者(signal handlers)(通過(guò)方法 connect()關(guān)聯(lián))。
解除一個(gè)接收器函數(shù)和一個(gè)信號(hào)的關(guān)聯(lián)。這跟方法 connect()有相反的作用,參數(shù)也相同。
取消給定信號(hào)綁定的所有接收器。
參數(shù):
模塊 scrapy.statscol 下有好幾種狀態(tài)收集器, 它們都實(shí)現(xiàn)了狀態(tài)收集器 API 對(duì)應(yīng)的類 Statscollector (即它們都繼承至該類)。
返回指定 key 的統(tǒng)計(jì)值,如果 key 不存在則返回缺省值。
以 dict 形式返回當(dāng)前 spider 的所有統(tǒng)計(jì)值。
設(shè)置 key 所指定的統(tǒng)計(jì)值為 value。
使用 dict 形式的 stats 參數(shù)覆蓋當(dāng)前的統(tǒng)計(jì)值。
增加 key 所對(duì)應(yīng)的統(tǒng)計(jì)值,增長(zhǎng)值由 count 指定。如果 key 未設(shè)置,則使用 start 的值設(shè)置為初始值。
如果 key 所對(duì)應(yīng)的當(dāng)前 value 小于參數(shù)所指定的 value,則設(shè)置 value。如果沒(méi)有 key 所對(duì)應(yīng)的 value,設(shè)置 value。
如果 key 所對(duì)應(yīng)的當(dāng)前 value 大于參數(shù)所指定的 value,則設(shè)置 value。如果沒(méi)有 key 所對(duì)應(yīng)的 value,設(shè)置 value。
清除所有統(tǒng)計(jì)信息。
以下方法不是統(tǒng)計(jì)收集 api 的一部分,但實(shí)現(xiàn)自定義的統(tǒng)計(jì)收集器時(shí)會(huì)使用到:
打開(kāi)指定 spider 進(jìn)行統(tǒng)計(jì)信息收集。
關(guān)閉指定 spider。調(diào)用后,不能訪問(wèn)和收集統(tǒng)計(jì)信息。