如何从项目管道访问临时设置 [英] How to access scrapy settings from item Pipeline
问题描述
如何从项目管道访问settings.py中的临时设置.文档中提到可以通过扩展程序中的搜寻器对其进行访问,但是我看不到如何在管道中访问该搜寻器.
How do I access the scrapy settings in settings.py from the item pipeline. The documentation mentions it can be accessed through the crawler in extensions, but I don't see how to access the crawler in the pipelines.
推荐答案
从your_spider.py
内部访问Scrapy设置(在settings.py
中定义)的方法很简单.所有其他答案都太复杂了.原因是对Scrapy文档的维护非常差,结合了许多最新的更新和内容.变化. 设置"文档"如何访问设置 ",也没有在设置API" 中进行设置,可行的例子.这是一个示例,说明如何获取当前的 USER_AGENT 字符串.
The way to access your Scrapy settings (as defined in settings.py
) from within your_spider.py
is simple. All other answers are way too complicated. The reason for this is the very poor maintenance of the Scrapy documentation, combined with many recent updates & changes. Neither in the "Settings" documentation "How to access settings", nor in the "Settings API" have they bothered giving any workable example. Here's an example, how to get your current USER_AGENT string.
只需将以下行添加到your_spider.py
:
# To get your settings from (settings.py):
from scrapy.utils.project import get_project_settings
...
class YourSpider(BaseSpider):
...
def parse(self, response):
...
settings = get_project_settings()
print "Your USER_AGENT is:\n%s" % (settings.get('USER_AGENT'))
...
如您所见,无需使用@classmethod
或重新定义from_crawler()
或__init__()
函数.希望这会有所帮助.
As you can see, there's no need to use @classmethod
or re-define the from_crawler()
or __init__()
functions. Hope this helps.
PS.我仍然不确定为什么使用from scrapy.settings import Settings
不能以相同的方式工作,因为这将是更明显的导入选择?
PS. I'm still not sure why using from scrapy.settings import Settings
doesn't work the same way, since it would be the more obvious choice of import?
这篇关于如何从项目管道访问临时设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!