[英]Is it possible to access stats from pipeline for a particular Spider in Scrapy?
我正在將Scrapy與多個Spider配合使用,並且需要自定義json輸出,其中將包含一些Spider統計信息(成功請求列表,錯誤列表等)。 我已經制作了自定義項目管道,但是我不知道如何從那里訪問統計信息。 到目前為止,這是我的管道代碼:
class JsonWithEncodingPipeline(object):
def open_spider(self, spider):
self.file = codecs.open(spider.output_path, 'w', encoding='utf-8')
def process_item(self, item, spider):
line = json.dumps(dict(item), ensure_ascii=False, indent=2) + "\n"
self.file.write(line)
return item
def spider_closed(self, spider):
self.file.close()
您可以像這樣訪問統計信息:
class MyPipeline:
def __init__(self, stats):
self.stats = stats
@classmethod
def from_crawler(cls, crawler):
return cls(crawler.stats)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.