python爬虫-UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
程序员文章站
2022-03-25 17:36:44
python爬虫-UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte ......
错误如下:
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",
上一篇: html常用标签大全
下一篇: react-路由和Ant design
推荐阅读
-
Anaconda中启动Python时的错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 553
-
命令行调用python出现编码错误:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x9a in position 140 完美解决!
-
Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
-
Python “'ascii' codec can't decode byte 0xb0i in position 1” 的解决方法
-
当在命令行中执行virtualenv venv时报此错误:'utf-8' codec can't decode byte 0xd5 in position 38: invalid continuation by
-
UnicodeDecodeError: 'utf-8' codec can't decode byte in position : invalid continuation byte
-
【debug】UnicodeDecodeError: codec can't decode byte 0xbd in position 4: invalid start byte
-
python3 报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6
-
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 0: invalid continuation byte
-
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte