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

标签 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)}

但是我想在蜘蛛运行完成后以代码访问整个字典。每当蜘蛛完成运行时,我都会执行此方法。
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}

最佳答案

我认为这可能是因为自 finish_reason 以来运行被中断或终止(?)是 shutdown而不是 finished在你的最后一个例子中。也许根本没有抓取任何元素,这就是为什么没有 item_scraped_count key ?

关于python - Scrapy Spider 关闭后如何获取 `item_scraped_count` - Python27,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41306669/

相关文章:

python - 使用 Robot Framework 截取整个网页的屏幕截图

python - 在 fastCGI apache 服务器上安装 Django 时无法找出错误日志

python - scrapy 的问题 - 没有抓取任何项目

Scrapy Splash 不会执行 lua 脚本

scrapy - 使用 Scrapy 增量爬取网站

xpath - 碎片 : Select tag with non-breaking space with xpath

python - 刮痧 : AttributeError: 'list' object has no attribute 'iteritems'

python - 将列表的相邻元素相乘并相加

python - 如何使用列表理解格式化列表列表?

javascript - Scrapy如何与Javascript打交道