Ubuntu12.04 urllib2打印时乱码问题,ubuntu12.04urllib2,代码如下:# -*- e
Ubuntu12.04 urllib2打印时乱码问题,ubuntu12.04urllib2,代码如下:# -*- e
代码如下:
# -*- encoding=utf-8 -*- import urllib2 import sys content = urllib2.urlopen('http://www.douban.com').read() type = sys.getfilesystemencoding() print content print content.decode("UTF-8").encode(type)
打印content的内容就是一堆乱七八糟的东西:��}isI��w�����,U�$��i���o�tOL��{_��)
(���b���q+٭}o˖e��M��E�7!�Eܟb�U��ᝬ*�Ul$��V@-�'3�~2O�--�_����������?�~�������-�CD��tyt��6}����xܣ���,��0+0�����Y���6�t�c
然后decode的时候又报错:UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: invalid start byte
系统环境是Ubuntu1204, Python2,7 ,这究竟是什么问题呢?
先谢谢了!
首先你的代码在我这里运行数据显示正常。不过根据你的输出来看,应该不是抓取到网页的编码错误(里面连ascii字符都没有,一般的乱码英文字符还是可以显示出来的)。猜测可能是你的源码文件编码错误。
另外,代码中说明源码文件编码,用coding:
# -*- coding=utf-8 -*-
还有type是内置类型,一般不要把变量名命名为python自带的类型名。
可能是压缩了,看下头里面是不是有 Content-Encoding:xxx
如果是压缩了,需要手动解压,urllib是不会帮你解压的
def unzip(self,data): import gzip import StringIO data = StringIO.StringIO(data) gz = gzip.GzipFile(fileobj=data) data = gz.read() gz.close() return data
很奇怪的问题,你用 logging 打下能否正常输出中文。
# -*- encoding=utf-8 -*- import loggingimport urllib2 import sys logging.basicConfig(level=logging.INFO)content = urllib2.urlopen('http://www.douban.com').read() logging.info(type(content)) logging.info(content.decode('utf-8'))
编橙之家文章,
相关内容
- Python方法获取得文件创建日期不正确是为什么?,pytho
- py2exe编译链接python代码报错ImportError,py2exeimporterror,使
- 请教python下载网页torrent文件的方法,pythontorrent,http:/
- Python统计网站流量及访客求交流,python访客,统计网站的
- python fabric批量管理多台server执行方法问题,pythonfabri
- 请问有Vim编辑器删除末尾空白字符操作快捷方式吗?,
- android解锁种数代码哪位python大神精通望指点,androidp
- 为什么我的for循环列表中最后一项总会被覆盖的原因是
- 我的python DHT11测温代码运行出错是如何解决,pythondht
- Python getattr报TypeError错误,getattrtypeerror,#-*-coding:u
评论关闭