一、问题

  request.Request(url,headers=headerDict))的时候出现异常信息:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

二、解决方案

  http头中headerDict参数: "Accept-Encoding": "gzip, deflate",

  代表本地可以接收压缩格式的数据,而服务器在处理时就将大文件压缩再发回客户端,IE在接收完成后在本地对这个文件又进行了解压操作。

  出错的原因是因为你的程序没有解压这个文件,所以删掉这行就不会出现问题。

https://blog.csdn.net/zhang_cl_cn/article/details/94575568

Python模块Requests的中文乱码问题

from:http://blog.csdn.net/chaowanghn/article/details/54889835

解决Requests中文乱码

都在推荐用Requests库,而不是Urllib,但是读取网页的时候中文会出现乱码。

分析: 
r = requests.get(“http://www.baidu.com“) 
**r.text返回的是Unicode型的数据。 
使用r.content返回的是bytes型的数据。 
也就是说,如果你想取文本,可以通过r.text。 
如果想取图片,文件,则可以通过r.content。**

获取一个网页的内容

方法1:使用r.content,得到的是bytes型,再转为str

url='http://music.baidu.com'
r = requests.get(url)
html=r.content
html_doc=str(html,'utf-8') #html_doc=html.decode("utf-8","ignore")
print(html_doc)

方法2:使用r.text 
Requests 会自动解码来自服务器的内容。大多数 unicode 字符集都能被无缝地解码。请求发出后,Requests 会基于 HTTP 头部对响应的编码作出有根据的推测。当你访问 r.text 之时,Requests 会使用其推测的文本编码。你可以找出 Requests 使用了什么编码,并且能够使用 r.encoding 属性来改变它. 
但是Requests库的自身编码为: r.encoding = ‘ISO-8859-1’ 
可以 r.encoding 修改编码

url='http://music.baidu.com'
r=requests.get(url)
r.encoding='utf-8'
print(r.text)

获取一个网页的内容后存储到本地

方法1:r.content为bytes型,则open时需要open(filename,”wb”)

r=requests.get("music.baidu.com")
html=r.content
with open('test5.html','wb') as f:f.write(html)

方法2:r.content为bytes型,转为str后存储

r = requests.get("http://www.baidu.com")
html=r.content
html_doc=str(html,'utf-8') #html_doc=html.decode("utf-8","ignore")
# print(html_doc)
with open('test5.html','w',encoding="utf-8") as f:f.write(html_doc)

方法3:r.text为str,可以直接存储

r=requests.get("http://www.baidu.com")
r.encoding='utf-8'
html=r.text
with open('test6.html','w',encoding="utf-8") as f:f.write(html)

Requests+lxml

# -*-coding:utf8-*-
import requests
from lxml import etreeurl="http://music.baidu.com"
r=requests.get(url)
r.encoding="utf-8"
html=r.text
# print(html)
selector = etree.HTML(html)
title=selector.xpath('//title/text()')
print (title[0])
结果为:百度音乐-听到极致

终极解决方法

以上的方法虽然不会出现乱码,但是保存下来的网页,图片不显示,只显示文本。而且打开速度慢,找到了一篇博客,提出了一个终极方法,非常棒。

来自博客 
http://blog.chinaunix.net/uid-13869856-id-5747417.html的解决方案:

# -*-coding:utf8-*-import requestsreq = requests.get("http://news.sina.com.cn/")if req.encoding == 'ISO-8859-1':encodings = requests.utils.get_encodings_from_content(req.text)if encodings:encoding = encodings[0]else:encoding = req.apparent_encoding# encode_content = req.content.decode(encoding, 'replace').encode('utf-8', 'replace')global encode_contentencode_content = req.content.decode(encoding, 'replace') #如果设置为replace,则会用?取代非法字符;print(encode_content)with open('test.html','w',encoding='utf-8') as f:f.write(encode_content)

Python3 解决 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte相关推荐

  1. Python3 使用requests请求,解码时出错:'utf8' codec can't decode byte 0x8b in position 1: invalid start byte...

    requests请求的响应内容能够通过几个属性获得: response.text 为解码之后的内容,解码会根据响应的HTTP Header中的Content-Type选择字符集.例如 "'C ...

  2. ‘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 sta ...

  3. UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0x8b in position 1: invalid start byte

    当我们使用urllib库打印爬取的网页信息print(res.read().decode('utf-8'))出现: UnicodeDecodeError: 'utf-8' codec can't de ...

  4. 'utf-8' codec can't decode byte 0x8b in position 2: invalid start byte

    用python的pandas模块中的pd.read_csv('Train_nyOWmfK.csv')函数读数据的时候遇到'utf-8' codec can't decode byte 0x8b in ...

  5. 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 报错原因 ...

  6. UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

    由于标题有字符限制没有办法将主题说的更明白,就将错误信息贴了上去,望谅解 场景:使用Python进行斗鱼直播页面的爬取,爬取的url:https://www.douyu.com/ 当使用str(爬取的 ...

  7. 解决 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

    原文:https://blog.csdn.net/zhanghaipeng1989/article/details/41117887 如果自己的脚本里编码没有问题的话,要看一下设置的header是否存 ...

  8. python3的web开发中出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid star

    本文全部是转载的: #--------------------------------------------下面是转载内容-------------------------------------- ...

  9. Python3解决UnicodeDecodeError:'utf-8' codec can't decode byte..问题终极解决方案

    0x00 问题引出: 最近在做一个买房自动化分析Python脚本,需要爬取网页. 在使用urllib获取reqest的response的时候,还要进行解码. 见语句: result = res.dec ...

最新文章

  1. BATJ 常考的 21 条 Linux 命令
  2. ArcGIS提高地图浏览效率
  3. springboot 第四讲
  4. Python time 100 天以后的日期
  5. 继续说invoke伪指令
  6. 搜狗输入法怎么清除输入记忆 搜狗输入法清除输入记忆方法
  7. vue复制html表格单列,vue表格含有一列多选框,如何获取被选中的行的数据?
  8. 基于matlab的16QAM调制解调仿真
  9. 计算机桌面的壁纸经典,电脑好看的励志的壁纸欣赏
  10. 如何搭建一对一直播PHP直播系统源码的流程
  11. HDU 3533 Escape
  12. 《Loy解说Eureka服务端源码(一)》
  13. CornerNet Guassian radius高斯半径的确定-数学公式详解
  14. 人造肌肉——双扭绳驱动结构
  15. PTA L1-039 python实现
  16. DTI预处理及确定性纤维束追踪
  17. 好看的登陆界面java_简单又美观的登录界面
  18. Android通过广播接收器BroadcastReceiver监听蓝牙连接变化
  19. latex写论文(TeXstudio工具)
  20. 沧小海笔记之基于xilinx的三速以太网相关知识的学习与理解

热门文章

  1. 校园网:认证程序无法继续初始化,绑定通信接口网络地址失败 问题的解决
  2. 从博客园搬家到csdn并能两边同步更新的方法
  3. 王济 matlab,关于王济老师《Matlab在振动信号处理中的应用》书中第八章8.3节 程序8.2a的问题...
  4. SwiftUI 2.0 课程笔记 Chapter 8
  5. 数据结构--树链剖分详解
  6. 曹建农院士:未来边缘计算:趋于分布式智能
  7. 【H5】HTML网页的基本结构
  8. spring-boot-2.0.3启动源码篇四 - run方法(三)之createApplicationContext
  9. VR沉浸感和交互作用原理
  10. 服务器硬件维保作用,服务器硬件管理和维护方法