获取response.text后,会出现以下乱码:
解决方法有两种。
第一个:
#一种从#response.apparent_encoding内容中分离响应内容的编码方法
response.encoding=response.apparent _ encoding
apparent_encoding方法是requests库使用chardet对字节流编码的猜测。 一般来说,使用这种方法可以恢复大多数网页的中文乱码。 如果仍然不行,试试第二种方法。
第二个:
#
text=response.content.decode (' utf-8 ',' ignore ' )。
从网页格式获取网页内容的编码方法。 典型的content-type字段显示在HTML头几行中。
直接拿来的话,我会报告错误的
unicodedecodeerror : ' utf-8 ' codec can ' tdecodebyte0x B6 in position 35363360 invalid start byte
因此,必须将第二个参数decode编码]、[错误='严格' ] )添加到decode中。 如果将第二个参数设置为ignore,则会忽略非法字符。
这样就能得到正确的中文内容。