【发布时间】:2016-09-15 13:26:03
【问题描述】:
我无法找到该问题的答案。 scrapy蜘蛛退出后如何执行python代码:
我在解析响应的函数中执行了以下操作 (def parse_item(self, response):) : self.my_function() 比我定义的 my_function(),但问题是它仍然在蜘蛛的循环中。我的主要想法是使用收集的数据在蜘蛛循环之外的函数中执行给定的代码。 谢谢。
【问题讨论】:
我无法找到该问题的答案。 scrapy蜘蛛退出后如何执行python代码:
我在解析响应的函数中执行了以下操作 (def parse_item(self, response):) : self.my_function() 比我定义的 my_function(),但问题是它仍然在蜘蛛的循环中。我的主要想法是使用收集的数据在蜘蛛循环之外的函数中执行给定的代码。 谢谢。
【问题讨论】:
使用Scrapy类的函数closed如下:
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)
【讨论】:
self.attributename 访问方法 closed() 中的那些属性