gpt4 book ai didi

python - 是否可以从 Scrapy 中的特定 Spider 的管道中访问统计信息?

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:09 24 4
gpt4 key购买 nike

我正在将 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)

关于python - 是否可以从 Scrapy 中的特定 Spider 的管道中访问统计信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29921884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com