python 网络爬虫报错“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position”解决方案
Python3.x爬虫,
发现报错“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1:invalid start byte”,
方法一:
一直在找文件的错误,最后经过网友的提示,错误原因竟然是我的报头中有一条:
“'Accept-Encoding': 'gzip, deflate'”
这一条是我从Fiddler直接复制过来的,为什么用浏览器可以正常浏览,而用Python模仿就不行呢?
综合网上的解释:
这句话的意思是本地接收压缩格式的数据,服务器传过来压缩格式gzip的文件,而解压这种gzip文件只能用deflate算法,浏览器能够自动解压,程序却不能自动解压gzip,需要额外进行设置才行,设置方法参考https://www.crifan.com/set_accept_encoding_header_to_gzip_deflate_return_messy_code/
总结:写爬虫程序时候还是不要写'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")
python 网络爬虫报错“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position”解决方案的更多相关文章
- python 3以上版本使用pickle.load读取文件报UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6
python 3以上版本使用pickle.load读取文件报UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6 ...
- 解决python错误 UnicodeDecodeError: 'gb2312' codec can't decode byte 0x8b in position 1: illegal multibyte sequence
报错的代码: url= 'http://kaijiang.500.com/shtml/ssq/19001.shtml' page =urllib.request.urlopen(url) conten ...
- 爬虫获取网页数据,报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start by
https://blog.csdn.net/hj_xy_0705/article/details/85011072
- 【python】UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
header中干掉 "Accept-Encoding": "gzip, deflate, br", 注意:
- python处理字符串时出现的错误'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)" 解决方法
解决办法,在该python文件的前面加上如下几句,问题得到解决. import sys default_encoding = 'utf-8' if sys.getdefaultencoding() ! ...
- 使用Python遇到:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte 问题
查看你的HTTP头部是否有如下头部信息:"Accept-Encoding": "gzip, deflate" 这条信息代表本地可以接收压缩格式的数据,而服务器在 ...
- 【python-HTMLTestRunner】生成HTMLTestRunner报告报错ERROR 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
[python-HTMLTestRunner]生成HTMLTestRunner报告报错:ERROR 'ascii' codec can't decode byte 0xe5 in position 0 ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xbb in position 51: ord
1.问题描述:一个在Django框架下使用Python编写的定时更新项目,在Windows系统下测试无误,在Linux系统下测试,报如下错误: ascii codec can't decode byt ...
- 【python】python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence
python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte ...
随机推荐
- 数值格式化 NumberFormat、 DecimalFormat、 RoundingMode
NumberFormat [简介] java.text.NumberFormat extends java.text.Format extends java.lang.Object 实现的接口:Ser ...
- 微信小程序-Q爱天气
最近找工作,发现微信小程序横行无忌,所以花两天时间把之前的一个天气小demo捡起来,重新上线发布一下.明天更新出小程序的讲解教程.
- 在CentOS下利用Docker一键安装seafile
https://cloud.seafile.com/published/seafile-manual-cn/docker/pro-edition/%E7%94%A8Docker%E9%83%A8%E7 ...
- LR脚本信息函数-lr_start_timer和lr_end_timer
为了计算时间更加精确,可以用这个函数去掉LR自身的检查点所浪费的时间.如text check and image time Action() { double time_elapsed, durati ...
- numpy(深)复制一个矩阵的方法
在用Python写代码的时候往往会遇到真复制和假复制的问题,真复制就是创建一个新的实例(instance),而假复制就是把原对象的引用赋给了新的标志符.判断是不是真复制可以使用id()这个函数. 当然 ...
- 破解版BrupSuite安装及其问题解决及环境部署
一 下载 BrupSuite_pro_v1.7.37的压缩包百度网盘链接: https://pan.baidu.com/s/1KkuseybjpuHo-6V4_wh9vw 提取码: 3vcs 说明一下 ...
- Flutter学习笔记(33)--GestureDetector手势识别
如需转载,请注明出处:Flutter学习笔记(33)--GestureDetector手势识别 这篇随笔主要记录的学习内容是GestureDetector手势识别,内容包括识别单击.双击.长按.组件拖 ...
- snprintf和sprintf区别分析
目录[-] snprintf函数的返回值 snprintf函数的字符串缓冲 今天在项目中使用snprintf时遇到一个比较迷惑的问题,追根溯源了一下,在此对sprintf和snprintf进行一下对比 ...
- excel如何快速汇总多个类别的总和?
这个需求是一位在当前抗疫一线的朋友提出的,和各位分享一下. 需求情况 因为众所周知的原因,他每天都需要为照顾的小区居民购买.运送生活物资.小区居民通过表单的形式提交自己每日的需求,最终汇总到一张exc ...
- 7、struct2的命名空间
采用命名空间可以区分不同action下面相同的函数名称 我们来看下面的一个程序的代码 我们来看下面的代码: 添加物料的action处理类: package com.weiyuan.test; publ ...