Python Scrapy - 在蜘蛛退出后执行代码

Pav*_*aev 1 python scrapy

我无法找到该问题的答案.scrapy蜘蛛退出后如何执行python代码:

我在解析响应的函数中做了以下内容(def parse_item(self,response):):self.my_function()我定义了my_function(),但问题是它仍然在蜘蛛的循环中.我的主要想法是使用收集的数据在蜘蛛循环外的函数中执行给定代码.谢谢.

Evh*_*vhz 8

使用Scrapy类关闭的函数如下:

class MySpider(scrapy.Spider):
    # some attributes
    spider_attr=[]

    def parse(self, response):
        # do your logic here
        # page_text = response.xpath('//text()').extract()
        self.spider_attr.append(whatever)

    def closed( self, reason ):
        # will be called when the crawler process ends
        # any code 
        # do something with collected data 
        for i in self.spider_attr: 
            print i
Run Code Online (Sandbox Code Playgroud)