scrapy: Call a function when a spider quits

后端 未结 6 1161
猫巷女王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:16

    if you have many spiders and want to do something before each of them closing, maybe it will be convenient to add statscollector in your project.

    in settings:

    STATS_CLASS = 'scraper.stats.MyStatsCollector'
    

    and collector:

    from scrapy.statscollectors import StatsCollector
    
    class MyStatsCollector(StatsCollector):
        def _persist_stats(self, stats, spider):
            do something here
    

提交回复
热议问题