在Scrapy 2.0.1中,我正在将新数据写入JSON文件。在进程结束时,我想要添加Scrapy统计信息。现在我知道有一个可用的Scrapy统计集合:
https://docs.scrapy.org/en/latest/topics/stats.html
因此,正确的代码行可能是:stats.get_stats()
结合使用:
class ExtensionThatAccessStats(object):
def __init__(self, stats):
self.stats = stats
@classmethod
def from_crawler(cls, crawler):
return cls(crawler.stats)
我目前的流程看起来像这样:
class test_pipeline(object):
file = None
def open_spider(self, spider):
self.file = open('data/test.json', 'wb')
self.exporter = JsonItemExporter(self.file)
self.exporter.start_exporting()
def close_spider(self, spider):
self.exporter.finish_exporting()
self.file.close()
我是Python新手。我该如何添加此功能才能将统计信息附加到json文件中?
from_crawler
。 - Gallaecio__init__
和from_crawler
方法添加到你的管道类中,从而使self.stats
可用。 - Gallaecio