python urllib2遇到Content-Encoding=gzip解码为乱码的解决方案
来源:互联网 发布:2016年7月淘宝新政策 编辑:程序博客网 时间:2024/06/14 11:06
python urllib2遇到Content-Encoding=gzip解码为乱码的解决方案
用Chrome的开发者工具,查看网页的headers,如果response headers出现Content-Encoding : gzip,则urllib2无法对其内容进行解码。
需要用gzip模块来处理,详细方法如下:
yresponse = urllib2.urlopen(url)rspheaders = yresponse.info()yread = yresponse.read()if ('Content-Encoding' in rspheaders and rspheaders['Content-Encoding'] == 'gzip') or ('content-encoding' in rspheaders and rspheaders['content-encoding'] == 'gzip'):import gzipimport StringIOydata = StringIO.StringIO(yread)ygz = gzip.GzipFile(fileobj = ydata)yread = ygz.read()ygz.close()ystr = yread.decode('utf8', 'ignore').encode('GB2312')else:ystr = yread.decode('utf8', 'ignore').encode('GB2312')
代码中url是需要访问的网址,decode()和encode()根据实际情况确定。
阅读全文
0 0
- python urllib2遇到Content-Encoding=gzip解码为乱码的解决方案
- Python下载文件时出现乱码的解决方法之一:Content-Encoding: gzip
- python以gzip header请求html数据时,response内容乱码无法解码的解决方案
- python以gzip header请求html数据时,response内容乱码无法解码的解决方案
- 使用 python urllib2 抓取网页时出现乱码的解决方案
- http: 支持 Content-Encoding: gzip
- 设置Accept-Encoding为gzip,deflate,返回的网页是乱码
- httpclient访问网站时设置Accept-Encoding为gzip,deflate返回的结果为乱码的问题
- C# 利用HttpWebRequest模拟登陆获取数据设置Accept-Encoding为gzip,deflate后返回的网页是乱码处理
- tomcat和nginx设置content-Encoding:gzip
- Python encoding error 解决方案
- 如何解析Content-Encoding: gzip服务器返回信息
- python解决urllib2乱码问题
- 【python】解决urllib2乱码问题
- Content-Encoding与Content-Type的区别
- Python的urllib2模块
- Python的urllib2模块
- Python的urllib2
- Linux/MacOS 中同时安装OpenCV2和OpenCV3并快速切换
- 湖北民族学院oj1800之 矩形与圆
- Java变量名的命名方式
- Kotlin 成了 Android 开发的官方语言
- 笔记:Android自定义按钮涟漪效果
- python urllib2遇到Content-Encoding=gzip解码为乱码的解决方案
- JUnit 4 与 TestNG 对比
- prepare hdf5 data for training
- vue成长之路+实战+Vue2+VueRouter2+webpack(一)vue-router路由入门
- POJ-2229-Sumsets -(简单dp)
- Java反射
- 湖北民族学院oj1829之 软件版本
- Policy Gradient Methods in Reinforcement Learning
- Python学习之函数(全局变量和局部变量)