如何从项目管道访问临时设置 [英] How to access scrapy settings from item Pipeline

查看:72
本文介绍了如何从项目管道访问临时设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从项目管道访问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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆