python使用response.read()读取请求结果时遇到中文会产生乱码,有以下几种可能:
1) IDE问题,我使用的是pycharm,在控制台中输出中文时产生乱码,此时需要将pycharm的编码方式改为'GBK'即可
2) reponse中headers包含'Content-Encoding:gzip'内容,表示服务器返回的结果时经过压缩的,因此需要对内容进行解压缩:
如下代码:
if 'gzip' in response.headers.get('Content-Encoding'): zlib.decompress(response.read(), 16 + zlib.MAX_WBITS)