scrapy: Call a function when a spider quits

后端 未结 6 1155
猫巷女王i
猫巷女王i 2020-11-27 14:29

Is there a way to trigger a method in a Spider class just before it terminates?

I can terminate the spider myself, like this:

class MySpider(CrawlS         


        
6条回答
  •  执念已碎
    2020-11-27 15:12

    For me the accepted did not work / is outdated at least for scrapy 0.19. I got it to work with the following though:

    from scrapy.signalmanager import SignalManager
    from scrapy.xlib.pydispatch import dispatcher
    
    class MySpider(CrawlSpider):
        def __init__(self, *args, **kwargs):
            super(MySpider, self).__init__(*args, **kwargs)
            SignalManager(dispatcher.Any).connect(
                self.closed_handler, signal=signals.spider_closed)
    
        def closed_handler(self, spider):
            # do stuff here
    

提交回复
热议问题