之前爬虫访问百度一直出现 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
然后安装了能够检测网页编码的chardet,安装之后检测了www.iqiyi.com,可以正常使用。
等到检测百度时,出现了chardet 获的encoding为none,然后又去查原因。
最终得知是从服务器或取得时压缩包,需要解压:
import zlib
import chardet
response=urllib.request.urlopen(url)
html=response.read()
html = zlib.decompress(html, 16+zlib.MAX_WBITS)#解压网页
print(chardet.detect(html)) #输出chardet读取的信息
html=html.decode("utf-8")