python请求模块记录编码
我使用的是python和请求模块==2.18.4 在使用请求爬网一些数据时,我使用日志模块进行调试 我希望日志看起来像这样: 但我明白了:python请求模块记录编码,python,logging,python-requests,Python,Logging,Python Requests,我使用的是python和请求模块==2.18.4 在使用请求爬网一些数据时,我使用日志模块进行调试 我希望日志看起来像这样: 但我明白了: [DEBUG] 2018-01-25 03:15:36,940 http://localhost:8888 "GET /aaa" 200 2290 [DEBUG] 2018-01-25 03:15:36,974 EUC-JP Japanese prober hit error at byte 1765 [DEBUG] 2018-01-25 03:15:36
[DEBUG] 2018-01-25 03:15:36,940 http://localhost:8888 "GET /aaa" 200 2290
[DEBUG] 2018-01-25 03:15:36,974 EUC-JP Japanese prober hit error at byte 1765
[DEBUG] 2018-01-25 03:15:36,990 EUC-KR Korean prober hit error at byte 1765
[DEBUG] 2018-01-25 03:15:36,994 CP949 Korean prober hit error at byte 1765
[DEBUG] 2018-01-25 03:15:37,009 EUC-TW Taiwan prober hit error at byte 1765
[DEBUG] 2018-01-25 03:15:37,036 utf-8 not active
[DEBUG] 2018-01-25 03:15:37,036 SHIFT_JIS Japanese confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,036 EUC-JP not active
[DEBUG] 2018-01-25 03:15:37,036 GB2312 Chinese confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,036 EUC-KR not active
[DEBUG] 2018-01-25 03:15:37,036 CP949 not active
[DEBUG] 2018-01-25 03:15:37,036 Big5 Chinese confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,036 EUC-TW not active
[DEBUG] 2018-01-25 03:15:37,036 windows-1251 Russian confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 KOI8-R Russian confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 ISO-8859-5 Russian confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 MacCyrillic Russian confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 IBM866 Russian confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 IBM855 Russian confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 ISO-8859-7 Greek confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 windows-1253 Greek confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 ISO-8859-5 Bulgairan confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 windows-1251 Bulgarian confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 TIS-620 Thai confidence = 0.01
[DEBUG] 2018-01-25 03:15:37,038 ISO-8859-9 Turkish confidence = 0.47949350706
[DEBUG] 2018-01-25 03:15:37,038 windows-1255 Hebrew confidence = 0.0
[DEBUG] 2018-01-25 03:15:37,038 windows-1255 Hebrew confidence = 0.0
[DEBUG] 2018-01-25 03:15:37,038 windows-1255 Hebrew confidence = 0.0
...
我不想在日志中使用这种编码。如何删除它们?尝试将模块
chardet.charsetprober
的日志记录级别设置为高于DEBUG的级别(例如INFO)
不确定我是否正确理解了您的问题,为什么您不能将此消息分为不同的日志级别。我遇到了相同的问题,并发现这些是来自模块的额外日志 要抑制这些日志,请将其置于导入之后
logging.getLogger('chardet.charsetprober').setLevel(logging.INFO)
这不会从模块打印任何DEBUG
level消息,您将只获得所需的日志消息
希望有帮助
响应.内容.解码('ISO-8859-1')
将其设置为混合字符集解码,对我有效我假设此问题与
r.text
有关(返回响应的text
属性)。由于请求
不知道具体的编码,它必须尝试一下,因此长列表被记录下来。为了避免这种情况,您可以在访问r.text之前将日志记录级别设置得更高(如INFO
),或者指定编码(r.encoding='utf-8'
或任何您喜欢的内容),如其他答案中所述,当您调用r.text
字段时,请求库会尝试猜测文本的编码
在某些情况下,您可以使用r.content
字段(二进制响应内容)而不是r.text
来避免这种猜测过程。这是请求的自动日志模块。。。我不能把它和我自己的分开。
logger = logging.getLogger('chardet.charsetprober')
logger.setLevel(logging.INFO)
logging.getLogger('chardet.charsetprober').setLevel(logging.INFO)