我无法找到该问题的答案。如何在 scrapy 蜘蛛退出后执行 python 代码:
我在解析响应的函数中执行了以下操作 (def parse_item(self, response):): self.my_function() 函数 比我定义的 my_function(),但问题是它仍然在蜘蛛的循环内。我的主要想法是使用收集到的数据在蜘蛛循环之外的函数中执行给定代码。 谢谢。
请您参考如下方法:
使用函数 closed 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)
