当前位置: 移动技术网 > IT编程>脚本编程>Python > python爬虫-UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

python爬虫-UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

2020年03月22日  | 移动技术网IT编程  | 我要评论

陈道学,qq管家dnf礼包,义乌汽车站

错误如下:

unicodedecodeerror: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

抓取的网页检查:

content-encoding: gzip

需要做gzip的解压

request = urllib.request.request(url = url, headers = request_headers)
reponse = urllib.request.urlopen(request,timeout = timeout)
data = reponse.read()
buff = bytesio(data)
f = gzip.gzipfile(fileobj=buff)
res = f.read().decode('utf-8')
print(res)

在请求的头部加入:"accept-encoding":"gzip",

如果是下面:则每次返回有可能是gzip压缩,有可能不压缩,web 应用干脆为了迁就 ie 直接输出原始 deflate

accept-encoding: gzip, deflate在请求的头部加入:

"accept-encoding":"gzip",

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网