python - urllib2 乱码问题
伊谢尔伦
伊谢尔伦 2017-04-17 11:19:46
[Python讨论组]

代码如下:

# -*- 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 ,这究竟是什么问题呢?
先谢谢了!

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(3)
怪我咯

可能是压缩了,看下头里面是不是有 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 logging
import 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')) 
伊谢尔伦

首先你的代码在我这里运行数据显示正常。不过根据你的输出来看,应该不是抓取到网页的编码错误(里面连ascii字符都没有,一般的乱码英文字符还是可以显示出来的)。猜测可能是你的源码文件编码错误。

另外,代码中说明源码文件编码,用coding:

# -*- coding=utf-8 -*-

还有type是内置类型,一般不要把变量名命名为python自带的类型名。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号