如何在Scrapy Spider关闭后获取`item_scraped_count` - Python27

时间:2016-12-23 19:04:08

标签: python scrapy scrapy-spider

每当s Spider完成后,scrapy会在终端/ CLI中显示它。

{'downloader/exception_count': 2,
 'downloader/exception_type_count/twisted.internet.error.TimeoutError': 2,
 'downloader/request_bytes': 58010,
 'downloader/request_count': 157,
 'downloader/request_method_count/GET': 157,
 'downloader/response_bytes': 1151051,
 'downloader/response_count': 155,
 'downloader/response_status_count/200': 136,
 'downloader/response_status_count/403': 19,
 'file_count': 9,
 'file_status_count/downloaded': 1,
 'file_status_count/uptodate': 8,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2016, 12, 23, 15, 8, 36, 325285),
 'item_scraped_count': 10,
 'log_count/DEBUG': 203,
 'log_count/ERROR': 57,
 'log_count/INFO': 289,
 'log_count/WARNING': 1354,
 'request_depth_max': 3,
 'response_received_count': 135,
 'scheduler/dequeued': 156,
 'scheduler/dequeued/memory': 156,
 'scheduler/enqueued': 156,
 'scheduler/enqueued/memory': 156,
 'spider_exceptions/AttributeError': 1,
 'start_time': datetime.datetime(2016, 12, 23, 15, 0, 9, 832308)}

但是当蜘蛛完成运行时,我想在代码中访问整个Dictionary。我只有在蜘蛛完成运行时执行此方法。

def spider_closed(self, spider):
    stats = spider.crawler.stats.get_stats() 
    self.logger.info("Spider is being closed")
    self.logger.info(stats)

它的输出不包括item_scraped_count,而是它的输出对我来说非常无用。它的输出是

{'log_count/DEBUG': 40, 'scheduler/dequeued': 28, 'log_count/INFO': 74, 'downloader/response_count': 28, 'downloader/response_status_count/200': 24, 'response_received_count': 28, 'scheduler/enqueued/memory': 425, 'downloader/response_bytes': 273395, 'finish_reason': 'shutdown', 'start_time': datetime.datetime(2016, 12, 23, 18, 20, 21, 580932), 'scheduler/dequeued/memory': 28, 'scheduler/enqueued': 425, 'downloader/response_status_count/403': 4, 'finish_time': datetime.datetime(2016, 12, 23, 18, 21, 20, 867566), 'downloader/request_bytes': 9566, 'request_depth_max': 2, 'log_count/WARNING': 1, 'downloader/request_method_count/GET': 28, 'downloader/request_count': 28}

1 个答案:

答案 0 :(得分:0)

我认为可能是因为运行被中断或终止(?),因为finish_reasonshutdown而不是上一个示例中的finished。也许根本没有刮掉任何物品,这就是为什么没有item_scraped_count键的原因?