推荐学习书目
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
360safe
V2EX  ›  Python

请问 scrapy 出现 failure 的时候,如何执行 close_spider

  •  
  •   360safe · Jan 25, 2018 · 5067 views
    This topic created in 3050 days ago, the information mentioned may be changed or developed.
    最近用 scrapy,因为网站限制,经常抓到一定数量的时候出现:

    2018-01-25 23:12:42 [scrapy.core.scraper] ERROR: Error downloading <GET https://XXX.com/id=139988706>;: [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]


    IP 被限制了,这时 scrapy 会继续的去尝试抓取剩下的页面,耗费时间
    我是想如果发现这种情况,就主动发起 close_spider,结束抓取。
    请问大婶们,该在哪里进行配置?

    yield scrapy.Request(url,self.parse_item) ——这里可以吗?如何配置?
    7 replies    2018-01-27 08:29:23 +08:00
    NLL
        1
    NLL  
       Jan 25, 2018 via iPhone
    关注
    rabbbit
        2
    rabbbit  
       Jan 25, 2018
    raise CloseSpider()
    ChenJinluo
        3
    ChenJinluo  
       Jan 26, 2018   ❤️ 1
    scrapy.Request()有个参数 errback。比如 scrapy.Request(self.url, self.parse, errback=self.handle_error, dont_filter=True) ,当出现错误时就会调用 handle_error()这个函数,在里面关爬虫就可以了。 具体用法可以去官方文档看 Requests and Responses 一节,也可以通过搜索框搜索 errback。
    cz5424
        4
    cz5424  
       Jan 26, 2018 via Android
    同意楼上,也可以加一个中间件结束
    toono
        6
    toono  
       Jan 26, 2018
    赞同 5 楼的积极解决问题的方案
    360safe
        7
    360safe  
    OP
       Jan 27, 2018
    收到,谢谢大家。
    通过大家的回复,我学习到了,自己对 scrapy 的了解仅仅是点,应该好好学习官方文档形成一个大的知识面。

    5 楼的方案之前用过,因为价格原因,大大超出了预算。(我要抓取的数据是千万级反复过滤)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1154 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 18:00 · PVG 02:00 · LAX 11:00 · JFK 14:00
    ♥ Do have faith in what you're doing.