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

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

zhangsir3年前 (2022-10-27)python390

发现报错“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版权k3防采集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”解决方案” 的相关文章

如何向python 列表中添加元素

Python添加元素有三种方法:append、extend、insertappend:向列表添加元素,添加到尾部实例:list=[“my”,“name”,“is”,“mark”,“age”,18] print(“添加前:”,list) list.append(“test”) print(“添加...

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&R实践课程介绍:https://www.aihorizon.cn/1百度网盘地址: https://pan.baidu.com/s/1a743NTKFRjsgexMTagWooA?pwd=e39j动手使用Python进行自然语言处理(NLP)课程介绍:http...

python 实现快速扣背景图功能

一,实现快速扣背景图需要rembg这个三方库#引入rembg库 from rembg import remove #素材 input_path = 'input.jpg' #效果 output_path =&nbs...

python Tesseract 验证码识别训练流程

1、用jTessBoxEditor把要训练样本图片文件合并成tif文件(样本图片一定要为有效的格式图片)点击顶栏的Tools选项,选择Merge TIFF..   进入你要训练的样本图片所在的目录,点击Ctrl+Alt+A,选择所有图片点击打开,然后保存文件名到指定目录,我这里保...