推荐学习书目
› 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 · Sep 3, 2014 · 10886 views
    This topic created in 4425 days ago, the information mentioned may be changed or developed.
    比如我在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 replies  •  2014-09-11 13:51:29 +08:00
    hustluo
        1
    hustluo  
       Sep 11, 2014
    您好,我是华中科技大学毕业的。现在在做中小学在线教育,立志于打造一款中小学知识点视频词典,学生不懂的题,我这里都有讲解。我们的网站和APP第一版已经开发完成,www.tuomeng.com.cn 现在招募Python的技术合伙人和在线教育的运营合伙人,身边有合适的人还望推荐,或者自荐,谢谢。昨天一天一个人发了个帖子,400个初二以上的中学生加我QQ群,现在也有几十万的用户信息,我QQ360714168,希望能够详聊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1234 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 17:31 · PVG 01:31 · LAX 10:31 · JFK 13:31
    ♥ Do have faith in what you're doing.