鍍金池/ 教程/ Python/ 核心 API
Benchmarking
命令行工具(Command line tools)
下載器中間件(Downloader Middleware)
信號(Signals)
Telnet 終端(Telnet Console)
初窺 Scrapy
數(shù)據(jù)收集(Stats Collection)
Scrapyd
通用爬蟲(Broad Crawls)
Item Loaders
試驗(yàn)階段特性
Scrapy 入門教程
自動限速(AutoThrottle)擴(kuò)展
Settings
Scrapy 終端(Scrapy shell)
下載項(xiàng)目圖片
DjangoItem
調(diào)試(Debugging)Spiders
選擇器(Selectors)
Feed exports
Spiders Contracts
借助 Firefox 來爬取
Logging
Spiders
Ubuntu 軟件包
實(shí)踐經(jīng)驗(yàn)(Common Practices)
安裝指南
Item Exporters
擴(kuò)展(Extensions)
Items
Spider 中間件(Middleware)
異常(Exceptions)
例子
發(fā)送 email
架構(gòu)概覽
常見問題(FAQ)
Jobs:暫停,恢復(fù)爬蟲
核心 API
使用 Firebug 進(jìn)行爬取
Item Pipeline
Link Extractors
Web Service
調(diào)試內(nèi)存溢出

核心 API

新版功能。

該節(jié)文檔講述 Scrapy 核心 API,目標(biāo)用戶是開發(fā) Scrapy 擴(kuò)展(extensions)和中間件(middlewares)的開發(fā)人員。

Crawler API

Scrapy API 的主要入口是 Crawler 的實(shí)例對象,通過類方法 from_crawler 將它傳遞給擴(kuò)展(extensions)。該對象提供對所有 Scrapy 核心組件的訪問,也是擴(kuò)展訪問 Scrapy 核心組件和掛載功能到 Scrapy 的唯一途徑。

Extension Manager 負(fù)責(zé)加載和跟蹤已經(jīng)安裝的擴(kuò)展,它通過 EXTENSIONS 配置,包含一個所有可用擴(kuò)展的字典, 字典的順序跟你在 configure the downloader middlewares 配置的順序一致。

class scrapy.crawler.Crawler(spidercls, settings)

Crawler 必須使用 scrapy.spider.Spider 子類及 scrapy.settings.Settings 的對象進(jìn)行實(shí)例化

settings

crawler 的配置管理器。

擴(kuò)展(extensions)和中間件(middlewares)使用它用來訪問 Scrapy 的配置。

關(guān)于 Scrapy 配置的介紹參考這里 Settings。

API 參考 Settings。

signals

crawler 的信號管理器。

擴(kuò)展和中間件使用它將自己的功能掛載到 Scrapy。

關(guān)于信號的介紹參考信號(Signals)

API 參考 SignalManager。

stats

crawler 的統(tǒng)計信息收集器。

擴(kuò)展和中間件使用它記錄操作的統(tǒng)計信息,或者訪問由其他擴(kuò)展收集的統(tǒng)計信息。

關(guān)于統(tǒng)計信息收集器的介紹參考數(shù)據(jù)收集(Stats Collection)

API 參考類 StatsCollector class。

extensions

擴(kuò)展管理器,跟蹤所有開啟的擴(kuò)展。

大多數(shù)擴(kuò)展不需要訪問該屬性。

關(guān)于擴(kuò)展和可用擴(kuò)展列表器的介紹參考擴(kuò)展(Extensions)。

engine

執(zhí)行引擎,協(xié)調(diào) crawler 的核心邏輯,包括調(diào)度,下載和 spider。

某些擴(kuò)展可能需要訪問 Scrapy 的引擎屬性,以修改檢查(modify inspect)或修改下載器和調(diào)度器的行為, 這是該 API 的高級使用,但還不穩(wěn)定。

spider 正在爬取的 spider。該 spider 類的實(shí)例由創(chuàng)建 crawler 時所提供, 在調(diào)用 :meth:`crawl` 方法是所創(chuàng)建。
crawl(*args, **kwargs)

根據(jù)給定的 args , kwargs 的參數(shù)來初始化 spider 類,啟動執(zhí)行引擎,啟動 crawler。

返回一個延遲 deferred 對象,當(dāng)爬取結(jié)束時觸發(fā)它。

get(name, default=None)

獲取某項(xiàng)配置的值,且不修改其原有的值。

參數(shù):

  • name (字符串) – 配置名
  • default (任何) – 如果沒有該項(xiàng)配置時返回的缺省值
getbool(name, default=False)

return False 將某項(xiàng)配置的值以布爾值形式返回。比如,1'1',True 都返回True, 而 0,'0',FalseNone 返回 False。

比如,通過環(huán)境變量計算將某項(xiàng)配置設(shè)置為 '0',通過該方法獲取得到 False

參數(shù):

  • name (字符串) – 配置名 - default (任何) – 如果該配置項(xiàng)未設(shè)置,返回的缺省值
getint(name, default=0)

將某項(xiàng)配置的值以整數(shù)形式返回

參數(shù):

  • name (字符串) – 配置名 - default (任何) – 如果該配置項(xiàng)未設(shè)置,返回的缺省值
getfloat(name, default=0.0)

將某項(xiàng)配置的值以浮點(diǎn)數(shù)形式返回

參數(shù):

  • name (字符串) – 配置名 - default (任何) – 如果該配置項(xiàng)未設(shè)置,返回的缺省值
getlist(name, default=None)

將某項(xiàng)配置的值以列表形式返回。如果配置值本來就是 list 則將返回其拷貝。如果是字符串,則返回被 ”,” 分割后的列表。

比如,某項(xiàng)值通過環(huán)境變量的計算被設(shè)置為'one,two',該方法返回[‘one’, ‘two’]

參數(shù):

  • name (字符串) – 配置名 - default (任何) – 如果該配置項(xiàng)未設(shè)置,返回的缺省值
getdict(name, default=None)

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ù):

  • name (字符串) – 配置名 - default (任何) – 如果該配置項(xiàng)未設(shè)置,返回的缺省值
copy()

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.

freeze()

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.

frozencopy()

Return an immutable copy of the current settings.

Alias for a freeze() call in the object returned by copy()

SpiderManager API

class scrapy.spidermanager.SpiderManager

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.

from_settings(settings)

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

load(spider_name)

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.

find_by_request(request)

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

信號(Signals) API

class scrapy.signalmanager.SignalManager

connect(receiver, signal)

鏈接一個接收器函數(shù)(receiver function) 到一個信號(signal)。

signal 可以是任何對象,雖然 Scrapy 提供了一些預(yù)先定義好的信號, 參考文檔信號(Signals)

參數(shù):

  • receiver (可調(diào)用對象) – 被鏈接到的函數(shù)
  • signal (對象) – 鏈接的信號
send_catch_log(signal, **kwargs)

發(fā)送一個信號,捕獲異常并記錄日志。

關(guān)鍵字參數(shù)會傳遞給信號處理者(signal handlers)(通過方法 connect()關(guān)聯(lián))。

send_catch_log_deferred(signal, **kwargs)

send_catch_log()相似但支持返回 deferreds 形式的信號處理器。

返回一個 deferred,當(dāng)所有的信號處理器的延遲被觸發(fā)時調(diào)用。發(fā)送一個信號,處理異常并記錄日志。

關(guān)鍵字參數(shù)會傳遞給信號處理者(signal handlers)(通過方法 connect()關(guān)聯(lián))。

disconnect(receiver, signal)

解除一個接收器函數(shù)和一個信號的關(guān)聯(lián)。這跟方法 connect()有相反的作用,參數(shù)也相同。

disconnect_all(signal)

取消給定信號綁定的所有接收器。

參數(shù):

  • signal (object) – 要取消綁定的信號

狀態(tài)收集器(Stats Collector) API

模塊 scrapy.statscol 下有好幾種狀態(tài)收集器, 它們都實(shí)現(xiàn)了狀態(tài)收集器 API 對應(yīng)的類 Statscollector (即它們都繼承至該類)。

class scrapy.statscol.StatsCollector

get_value(key, default=None)

返回指定 key 的統(tǒng)計值,如果 key 不存在則返回缺省值。

get_stats()

以 dict 形式返回當(dāng)前 spider 的所有統(tǒng)計值。

set_value(key, value)

設(shè)置 key 所指定的統(tǒng)計值為 value。

set_stats(stats)

使用 dict 形式的 stats 參數(shù)覆蓋當(dāng)前的統(tǒng)計值。

inc_value(key, count=1, start=0)

增加 key 所對應(yīng)的統(tǒng)計值,增長值由 count 指定。如果 key 未設(shè)置,則使用 start 的值設(shè)置為初始值。

max_value(key, value)

如果 key 所對應(yīng)的當(dāng)前 value 小于參數(shù)所指定的 value,則設(shè)置 value。如果沒有 key 所對應(yīng)的 value,設(shè)置 value。

min_value(key, value)

如果 key 所對應(yīng)的當(dāng)前 value 大于參數(shù)所指定的 value,則設(shè)置 value。如果沒有 key 所對應(yīng)的 value,設(shè)置 value。

clear_stats()

清除所有統(tǒng)計信息。

以下方法不是統(tǒng)計收集 api 的一部分,但實(shí)現(xiàn)自定義的統(tǒng)計收集器時會使用到:

open_spider(spider)

打開指定 spider 進(jìn)行統(tǒng)計信息收集。

close_spider(spider)

關(guān)閉指定 spider。調(diào)用后,不能訪問和收集統(tǒng)計信息。

上一篇:選擇器(Selectors)下一篇:Spiders