在使用requests库时,response对象的.text属性
r.text
默认的response对象的encoding属性是None,所以在调用r.text解码的时候,request会猜它的编码
If Response.encoding is None, encoding will be guessed using chardet.
encoding = None
Encoding to decode with when accessing r.text.
r.content
如果暂时不清楚编码是什么,则可以调用.content属性,它返回的是自己(bytes),就是还没有编码。然后让bs4库来解析。
bs4 编码
https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id51
Beautiful Soup用了 编码自动检测 子库来识别当前文档编码并转换成Unicode编码. BeautifulSoup 对象的 .original_encoding 属性记录了自动识别编码的结果:
soup.original_encoding
参考文献:
http://docs.python-requests.org/zh_CN/latest/api.html#requests.Response.content