分享

python爬虫-UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in positio

 怡红公子0526 2020-06-17

错误如下:

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",

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多