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

关于 scrapy 中 signals 的用法请教

  •  
  •   saximi · Sep 27, 2017 · 3360 views
    This topic created in 3288 days ago, the information mentioned may be changed or developed.

    看了 scrapy 的官方文档,关于 signals 的函数在 from_crawler()中有如下示例代码:

    crawler.signals.connect(ext.spider_opened, signal=signals.spider_opened) #代码 1 
    crawler.signals.connect(ext.spider_closed, signal=signals.spider_closed) #代码 2  
    
    对应两个方法定义如下: 
    def spider_opened(self, spider): 
        spider.log("opened spider %s" % spider.name) 
    def spider_closed(self, spider): 
        spider.log("closed spider %s" % spider.name) 
    
    

    恳请大家指点,上面代码中和 signals 的这几个函数和方法到底是完成了什么工作呢? 谢谢!

    3 replies  •  2017-09-28 20:25:44 +08:00
    sunwei0325
        1
    sunwei0325  
       Sep 27, 2017
    主要是写 extension 用的, 配合几个 signal 当触发器用的, 具体可以看看内置的 extension 代码
    cszhiyue
        2
    cszhiyue  
       Sep 28, 2017
    这里首先注册两个函数 spider_opened, spider_closed。当 spider 启动和关闭的时候会触发。有点类似 wrapper 的概念
    比如爬虫结束可以执行一些清理的工作比如发邮件。
    saximi
        3
    saximi  
    OP
       Sep 28, 2017
    @cszhiyue
    @sunwei0325 非常感谢!!!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5416 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 03:26 · PVG 11:26 · LAX 20:26 · JFK 23:26
    ♥ Do have faith in what you're doing.