[英]How to set class variable through __init__ in Python?
I am trying to change setting from command line while starting a scrapy crawler (Python 3.7).我试图在启动爬虫爬虫(Python 3.7)时从命令行更改设置。 Therefore I am adding a init method, but I could not figure out how to change the class varible "delay" from within the init method.
因此,我添加了一个init方法,但我无法弄清楚如何从init方法中更改类变量“延迟”。
Example minimal:示例最小:
class testSpider(CrawlSpider):
custom_settings = {
'DOWNLOAD_DELAY': 10, # default value
}
""" get arguments passed over CLI
scrapyd usage: -d arg1=val1
scrapy usage: -a arg1=val1
"""
def __init__(self, *args, **kwargs):
super(testSpider, self).__init__(*args, **kwargs)
self.delay = kwargs.get('delay')
if self.delay:
testSpider.custom_settings['DOWNLOAD_DELAY'] = self.delay
print('init:', testSpider.custom_settings['DOWNLOAD_DELAY'])
print(custom_settings['DOWNLOAD_DELAY'])
This will not change the setting unfortunatelly:不幸的是,这不会改变设置:
scrapy crawl test -a delay=5
10
init: 5
How can the class variable be changed?如何更改类变量?
I am trying to change setting from command line while starting a scrapy crawler (Python 3.7).
我试图在启动爬虫爬虫(Python 3.7)时从命令行更改设置。 Therefore I am adding a init method...
因此我添加了一个 init 方法...
......
scrapy crawl test -a delay=5
According to scrapy docs.根据scrapy docs。 (Settings/Command line options section) it is requred to use
-s
parameter to update setting (设置/命令行选项部分)需要使用
-s
参数来更新设置scrapy crawl test -s DOWNLOAD_DELAY=5
It is not possible to update settings during runtime in spider code from init
or other methods (details in related discussion on github Update spider settings during runtime #4196无法在运行时通过
init
或其他方法更新蜘蛛代码中的设置(有关 github 的相关讨论中的详细信息在运行时更新蜘蛛设置 #4196
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.