核心API
- class
scrapy.crawler.
Crawler
(spidercls, settings)
Crawler必须使用 子类及scrapy.settings.Settings
的对象进行实例化settings
crawler的配置管理器。
扩展(extensions)和中间件(middlewares)使用它用来访问Scrapy的配置。
关于Scrapy配置的介绍参考这里 Settings。
API参考 。
signals
crawler的信号管理器。
扩展和中间件使用它将自己的功能挂载到Scrapy。
关于信号的介绍参考 。
API参考SignalManager
。
stats
crawler的统计信息收集器。
扩展和中间件使用它记录操作的统计信息,或者访问由其他扩展收集的统计信息。
关于统计信息收集器的介绍参考 数据收集(Stats Collection)。
API参考类StatsCollector
class。API see class.
configure
()
配置crawler。
该方法加载扩展、中间件和spiders,使crawler处于ready状态。同时,它还配置好了执行引擎。
scrapy.settings.
SETTINGSPRIORITIES
Dictionary that sets the key name and priority level of the defaultsettings priorities used in Scrapy.
Each item defines a settings entry point, giving it a code name foridentification and an integer priority. Greater priorities take moreprecedence over lesser ones when setting and retrieving values in theSettings
class.
For a detailed explanation on each settings sources, see:.
- _class
scrapy.settings.
Settings
(values={}, priority='project')
This object stores Scrapy settings for the configuration of internalcomponents, and can be used for any further customization.
After instantiation of this class, the new object will have the globaldefault settings described on alreadypopulated.
Additional values can be passed on initialization with thevalues
argument, and they would take thepriority
level. If the latterargument is a string, the priority name will be looked up inSETTINGS_PRIORITIES
. Otherwise, a expecificinteger should be provided.
Once the object is created, new settings can be loaded or updated with the method, and can be accessed with thesquare bracket notation of dictionaries, or with theget()
method of the instance and its valueconversion variants. When requesting a stored key, the value with thehighest priority will be retrieved.set
(name, value, priority='project')
Store a key/value attribute with a given priority.Settings should be populated before configuring the Crawler object(through theconfigure()
method),otherwise they won’t have any effect.
setdict
(values, priority='project')
Store key/value pairs with a given priority.
This is a helper function that callsset()
for every item ofvalues
with the provided .参数:
- values (dict) – the settings names and values
- priority (string or int) – the priority of the settings. Should be a key of or an integer
setmodule
(module, priority='project')
Store settings from a module with a given priority.
This is a helper function that calls for every globally declareduppercase variable ofmodule
with the providedpriority
.参数:
- module (module object or string) – the module or the path of the module
- priority (string or int) – the priority of the settings. Should be a key ofSETTINGS_PRIORITIES
or an integer
get
(name, default=None)
获取某项配置的值,且不修改其原有的值。参数:
- name (字符串) – 配置名
- default (任何) – 如果没有该项配置时返回的缺省值
getbool
(name, default=False)
returnFalse
将某项配置的值以布尔值形式返回。比如,1
和'1'
,True
都返回True
,而0
,'0'
,False
和None
返回False
。
比如,通过环境变量计算将某项配置设置为'0'
,通过该方法获取得到False
。
getint
(name, default=0)
将某项配置的值以整数形式返回参数:
- name (字符串) – 配置名
- default (任何) – 如果该配置项未设置,返回的缺省值
getlist
(name, default=None)
将某项配置的值以列表形式返回。如果配置值本来就是list则原样返回。如果是字符串,则返回被 ”,” 分割后的列表。
比如,某项值通过环境变量的计算被设置为'one,two'
,该方法返回[‘one’, ‘two’]。参数:
- name (字符串) – 配置名
- default (任何) – 如果该配置项未设置,返回的缺省值
getdict
(name, default=None)
Get a setting value as a dictionary. If the setting original type is adictionary, a copy of it will be returned. If it’s a string it willevaluated as a json dictionary.
copy
()
Make a deep copy of current settings.
This method returns a new instance of theSettings
class,populated with the same values and their priorities.
Modifications to the new object won’t be reflected on the originalsettings.
freeze
()
Disable further changes to the current settings.
After calling this method, the present state of the settings will becomeimmutable. Trying to change values through theset()
method andits variants won’t be possible and will be alerted.
frozencopy
()
Return an immutable copy of the current settings.
Alias for afreeze()
call in the object returned by
- class
scrapy.loader.
This class is in charge of retrieving and handling the spider classesdefined across the project.
Custom spider loaders can be employed by specifying their path in the project setting. They must fully implementthescrapy.interfaces.ISpiderLoader
interface to guarantee anerrorless execution.fromsettings
(_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 spidersfound in the modules of the setting.参数: settings ( Settings
instance) – project settings
load
(spider_name)
Get the Spider class with the given name. It’ll look into the previouslyloaded spiders for a spider class with name spider_name and will raisea KeyError if not found.参数: spider_name (str) – spider class name
findby_request
(_request)
List the spiders’ names that can handle the given request. Will try tomatch the request’s url against the domains of the spiders.参数: request ( Request
instance) – queried request
- class
scrapy.signalmanager.
SignalManager
connect
(receiver, signal)
链接一个接收器函数(receiver function) 到一个信号(signal)。
signal可以是任何对象,虽然Scrapy提供了一些预先定义好的信号,参考文档 。
sendcatch_log_deferred
(_signal, **kwargs)
跟 相似但支持返回 deferreds 形式的信号处理器。
返回一个 ,当所有的信号处理器的延迟被触发时调用。发送一个信号,处理异常并记录日志。
关键字参数会传递给信号处理者(signal handlers)(通过方法connect()
关联)。
disconnect
(receiver, signal)
解除一个接收器函数和一个信号的关联。这跟方法connect()
有相反的作用,参数也相同。
disconnectall
(_signal)
取消给定信号绑定的所有接收器。参数: signal (object) – 要取消绑定的信号
- class
scrapy.statscollectors.
StatsCollector
getvalue
(_key, default=None)
返回指定key的统计值,如果key不存在则返回缺省值。
set_value
(_key, value)
设置key所指定的统计值为value。
incvalue
(_key, count=1, start=0)
增加key所对应的统计值,增长值由count指定。如果key未设置,则使用start的值设置为初始值。
minvalue
(_key, value)
如果key所对应的当前value大于参数所指定的value,则设置value。如果没有key所对应的value,设置value。
以下方法不是统计收集api的一部分,但实现自定义的统计收集器时会使用到:open_spider
(_spider)
打开指定spider进行统计信息收集。