V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
pc10201
V2EX  ›  Python

在 scrapy 中,如何在 spider 覆盖掉 settings.py 中的配置

  •  
  •   pc10201 · 2014-09-03 10:45:38 +08:00 · 10051 次点击
    这是一个创建于 3521 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如我在settings.py中设置了

    USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0'

    在某个spider中想覆盖掉这个值

    我用了两种办法

    方法一

    from scrapy.conf import settings

    settings.set('USER_AGENT','firefox')

    方法二

    from scrapy.settings import Settings
    settings=Settings()
    settings.set('USER_AGENT','firefox')

    方法三
    from scrapy.utils.project import get_project_settings
    settings = get_project_settings()
    settings.set('USER_AGENT','firefox')

    方法二和方法三不起作用,方法一是生效的,但是from scrapy.conf import settings是被scrapy废弃掉了,可以兼容使用,但是官方不推荐
    会提示
    Module `scrapy.conf` is deprecated, use `crawler.settings` attribute instead

    我折腾了半天,没有用crawler.settings搞出来,大家怎么解决的?
    1 条回复    2014-09-11 13:51:29 +08:00
    hustluo
        1
    hustluo  
       2014-09-11 13:51:29 +08:00
    您好,我是华中科技大学毕业的。现在在做中小学在线教育,立志于打造一款中小学知识点视频词典,学生不懂的题,我这里都有讲解。我们的网站和APP第一版已经开发完成,www.tuomeng.com.cn 现在招募Python的技术合伙人和在线教育的运营合伙人,身边有合适的人还望推荐,或者自荐,谢谢。昨天一天一个人发了个帖子,400个初二以上的中学生加我QQ群,现在也有几十万的用户信息,我QQ360714168,希望能够详聊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3449 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:10 · PVG 19:10 · LAX 04:10 · JFK 07:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.