python3访问sina首页中文的处理方法
2014-11-08 00:00
627 查看
摘要: 如果用python3的urllib或python2的urllib2访问网页,都不能得到正确的中文字符串,看下面的解决方法
""" 如果只用普通的 import urllib.request html = urllib.request.urlopen("http://www.sina.com").read() print(html.decode('gbk')) 出现下面的错误 builtins.UnicodeDecodeError: 'gbk' codec can't decode byte 0x8b in position 1: illegal multibyte sequence 怎么办?原来是有的网站将网页用gzip压缩了 。 请看下面的代码 import io import urllib.request as r import gzip req = r.Request("http://www.sina.com", headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36", "Accept-Encoding": "gzip"}) bs = r.urlopen(req).read() bi = io.BytesIO(bs) gf = gzip.GzipFile(fileobj=bi, mode="rb") print(gf.read().decode("gbk"))
相关文章推荐
- python3访问sina首页中文的处理方法
- python3访问sina首页中文的处理方法
- 【随笔记录】python 处理中文时出现的错误'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)" 解决方法
- 中文编码相关,python处理gbk编码的xml文件方法
- python中读写文件及中文编码处理方法【整理】
- Python3 URLopen 含中文URL的处理方法
- easyUI icon中文命名图片无法在浏览器中访问处理方法
- python中文处理好方法
- Python requests库解析中文网页乱码处理方法
- PYTHON 2.2 IDLE 中文字串处理设定方法
- python处理csv文件名中中文字符解决方法
- linux 系统下python程序中出现中文的处理方法
- FTP服务器提示无法打开FTP文件夹是否有权限访问或者不能上传中文的处理方法
- python 处理中文时出现的错误'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)" 解决方法
- Python读写Json涉及到中文的处理方法
- sqlite3 jdbc、c接口、python接口处理中文时遇到的问题及其解决方法
- python中读写文件及中文编码处理方法
- python中文处理好方法
- Python处理中文文本字符时提取某个汉字或字符的方法
- 中文编码相关,python处理gbk编码的xml文件方法