当前位置:网站首页>Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间
Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间
2022-04-23 06:27:00 【阿兵哥哥】
一、问题背景
scrapy 在每次运行结束后都会显示一堆统计数据信息,其中是有统计时间数据的,但是!!!那个时间是 UTC 时间(0时区),不是我们平时习惯的系统本地时间,而且里面的爬虫总运行时间是以秒计算的,不符合我们的日常习惯,于是我翻了下scrapy 的源码,找到其中相关的内容并将其重写了一遍,感觉还行,各位看官随缘取之!
二、问题分析
通过日志信息,找到对应的统计爬虫运行时间的类:scrapy.extensions.corestats.CoreStats
- 日志信息显示如下:
# 扩展配置 2021-05-10 10:43:50 [scrapy.middleware] INFO: Enabled extensions: ['scrapy.extensions.corestats.CoreStats', # 信号收集器,里面有记录爬虫的运行时间信息 'scrapy.extensions.telnet.TelnetConsole', 'scrapy.extensions.logstats.LogStats'] # 统计信息 2021-05-10 10:44:10 [scrapy.statscollectors] INFO: Dumping Scrapy stats: { 'downloader/exception_count': 3, 'downloader/exception_type_count/twisted.internet.error.ConnectionRefusedError': 2, 'downloader/exception_type_count/twisted.internet.error.TimeoutError': 1, 'downloader/request_bytes': 1348, 'downloader/request_count': 4, 'downloader/request_method_count/GET': 4, 'downloader/response_bytes': 10256, 'downloader/response_count': 1, 'downloader/response_status_count/200': 1, 'elapsed_time_seconds': 18.806005, # 爬虫运行总耗时 'finish_reason': 'finished', 'finish_time': datetime.datetime(2021, 5, 10, 2, 44, 10, 418573), # 爬虫结束时间 'httpcompression/response_bytes': 51138, 'httpcompression/response_count': 1, 'log_count/INFO': 10, 'response_received_count': 1, 'scheduler/dequeued': 4, 'scheduler/dequeued/memory': 4, 'scheduler/enqueued': 4, 'scheduler/enqueued/memory': 4, 'start_time': datetime.datetime(2021, 5, 10, 2, 43, 51, 612568)} # 爬虫开始时间 2021-05-10 10:44:10 [scrapy.core.engine] INFO: Spider closed (finished) - 源码截图如下:

三、解决方法
-
重写
CoreStats类# -*- coding: utf-8 -*- # 重写信号收集器 import time from scrapy.extensions.corestats import CoreStats class MyCoreStats(CoreStats): def spider_opened(self, spider): """爬虫开始运行""" self.start_time = time.time() start_time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.start_time)) # 转化格式 self.stats.set_value('爬虫开始时间: ', start_time_str, spider=spider) def spider_closed(self, spider, reason): """爬虫结束运行""" # 爬虫结束时间 finish_time = time.time() # 转化时间格式 finish_time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(finish_time)) # 计算爬虫运行总耗时 elapsed_time = finish_time - self.start_time m, s = divmod(elapsed_time, 60) h, m = divmod(m, 60) self.stats.set_value('爬虫结束时间: ', finish_time_str, spider=spider) self.stats.set_value('爬虫运行总耗时: ', '%d时:%02d分:%02d秒' % (h, m, s), spider=spider) self.stats.set_value('爬虫结束原因: ', reason, spider=spider) -
修改配置文件信息
EXTENSIONS = { 'scrapy.extensions.corestats.CoreStats': None, # 禁用默认的数据收集器 '项目名.extensions.corestats.MyCoreStats': 500, # 启用自定义的信号收集器 }
四、效果展示
2021-05-10 11:11:03 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{
'downloader/exception_count': 5,
'downloader/exception_type_count/twisted.internet.error.ConnectionRefusedError': 3,
'downloader/exception_type_count/twisted.internet.error.TimeoutError': 2,
'downloader/request_bytes': 1976,
'downloader/request_count': 6,
'downloader/request_method_count/GET': 6,
'downloader/response_bytes': 10266,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'httpcompression/response_bytes': 51139,
'httpcompression/response_count': 1,
'log_count/INFO': 10,
'response_received_count': 1,
'scheduler/dequeued': 6,
'scheduler/dequeued/memory': 6,
'scheduler/enqueued': 6,
'scheduler/enqueued/memory': 6,
'爬虫结束原因': 'finished',
'爬虫开始时间: ': '2021-05-10 11:10:39',
'爬虫结束时间: ': '2021-05-10 11:11:03',
'爬虫运行总耗时: ': '0时:00分:24秒'}
版权声明
本文为[阿兵哥哥]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Ayue1220/article/details/116589332
边栏推荐
- SVG中Path Data数据简化及文件夹所有文件批量导出为图片
- 面经的总结
- Page dynamic display time (upgraded version)
- The difference and application of VR, AR and MR, as well as some implementation principles of AR technology
- 12. Constraints
- FSM有限状态机
- C# 多个矩形围成的多边形标注位置的问题
- instanceof的实现原理
- Super classic & Programming Guide (red and blue book) - Reading Notes
- 基于NLP的软件安全研究(一)
猜你喜欢

Redis connection error err auth < password > called without any password configured for the default user

SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)

BTREE, B + tree and hash index

SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)

如何判断点是否在多边形内(包含复杂多边形或者多边形数量很多的情况)

防抖和节流

SAP PI/PO rfc2Soap 发布rfc接口为ws示例

js之什么是事件?事件三要素以及操作元素

SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)

How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
随机推荐
防抖和节流
将单行文字自动适应到目标矩形框内
二叉树的深度
【自我激励系列】到底是什么真正阻碍了你?
设置了body的最大宽度,但是为什么body的背景颜色还铺满整个页面?
js中对象的三种创建方式
Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
判断字符串首尾是否包含目标参数:startsWith()、endsWith()方法
js之DOM学习三种创建元素的方式
keytool: command not found
SVG中Path Data数据简化及文件夹所有文件批量导出为图片
利用网页表格导出EXCEL表格加线框及表格内部间距的问题
Install and configure Taobao image NPM (cnpm)
基于NLP的软件安全研究(二)
King glory - unity learning journey
对js中argumens的简单理解
SAP TRANSLATE使用数据对象掩码示例
10. Update operation
How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
Authorization server (simple construction of authorization server)