无意中遇到的问题:
发起request请求正常,返回的text里中文部分全部乱码,其他都正常;
此处是content-type没有指定默认的编码,response默认是使用iso-8859-1编码对消息进行编码,再传送数据给客户端(大部分网页是有指定编码的)
下面的多个解决方案:
方法一:自行设置charset
# 在html中搜索“charset”,找到编码格式,如此处是:charset="gb2312"
prob_res = requests.get('http://www.*******.com/js/cTypeInfo.js?version=' + str(getdate()))
prob_res.encoding = 'gb2312'
print(prob_res.text)
方法二:使用apparent_encoding
res = requests.get(url, headers = header)
res.encoding = res.apparent_encoding
pritn(res.text)
方法三:已经爬好的带有\u5430的字符串怎么转为正常的中文显示字符串
str.encode(‘utf-8’).decode(‘unicode_escape’)