当前位置:首页 > python > 正文内容

python 爬虫 报错:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0x8b in position”解决方案

zhangsir3年前 (2022-10-27)python318

发现报错“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1:invalid start byte”,


方法一:


根据报错提示,错误原因有一条是这样的:

“'Accept-Encoding': 'gzip, deflate'”


网络上的解释是: 


这句话的意思是本地接收压缩格式的数据,服务器传过来压缩格式gzip的文件,而解压这种gzip文件只能用deflate算法,浏览器能够自动解压,程序却不能自动解压gzip。


总结:写爬虫程序时候还是不要写'Accept-Encoding': 'gzip, deflate'了,就让服务器传原始文件过来吧,不用压缩了。


方法二:


利用gzip进行解压缩。


python3 :

import gzip
import urllib.request
response_1 = urllib.request.urlopen('').read()
data=gzip.decompress(response_1).decode("utf-8")


zhangsir版权y1防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:https://mianka.xyz/post/52.html

标签: python爬虫
分享给朋友:

“python 爬虫 报错:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0x8b in position”解决方案” 的相关文章

在Linux操作系统上使用selenium库

安装selenium模块命令:pip3 install selenium安装chrome命令:yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64....

Python三方库ddddocr实现验证码识别

Python三方库ddddocr实现验证码识别环境要求python >= 3.8安装三方库pip install ddddocr -i https://pypi.tuna.tsinghua.edu.cn/simple参数说明:参数名参数类型默认值说明us...

pip安装三方库 国内的一些镜像站点推荐

pip 国内的一些镜像站点推荐镜像套路:使用cmd;输入命令pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple 包名 即可开始安装。清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http...

解决Django的request.POST获取不到请求参数的问题

这个是Django自身的问题:只要在请求头的添加"content-type":'application/x-www-form-urlencoded'就行。...

python 给电脑设置闹钟

python会自动触发windows桌面通知,提示重要事项,比如说:您已工作两小时,该休息了我们可以设定固定时间提示,比如隔10分钟、1小时等用到的第三方库:win10toast - 用于发送桌面通知的工具from win10toast import ToastNoti...

python 使用PIL库进行验证码清晰处理

python 使用PIL库进行验证码清晰处理from PIL import Image import sys import os sys.setrecursionlimit(1000000) pixel_list = []...