|  |      1felix021      2013-08-15 20:44:53 +08:00  3 print res.read().decode('utf-8').encode('cp936') | 
|      3timonwong      2013-08-15 21:19:49 +08:00 @itfanr  无输出,两种可能 1. read() 不能读两次 2. 这个网页包含了一些稀有字符,因此要 print res.read().decode('utf-8').encode('gb18030') | 
|  |      4ling0322      2013-08-15 21:22:27 +08:00 1. 去看看print res.read().decode('gbk') 2. 这个response网页是不是被gzip压缩过 | 
|  |      5cute      2013-08-15 21:25:02 +08:00 import sys import urllib2 url = 'http://mlook.mobi/book/info/6248' res = urllib2.urlopen(url) s = res.read() print s.decode('utf8').encode(sys.stdout.encoding) | 
|  |      6ccdjh      2013-08-15 21:33:45 +08:00 这个么? import sys default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) | 
|  |      9zippera OP | 
|      10cj1324      2013-08-15 22:16:57 +08:00 你们都没有试用编码识别工具的习惯吗? | 
|  |      11manoon      2013-08-15 22:26:18 +08:00 先wget掉。 然后在本地读取HTML来测试。 | 
|      12nulloo      2013-08-15 22:28:31 +08:00 cygwin没怎么用过,反正我尽量不用win的终端输出中文,宁可用ide或者ssh,重定向py的输出到文件,用编辑器看看编码对不,再就是检查环境变量 | 
|  |      13zippera OP | 
|  |      14manoon      2013-08-15 22:44:06 +08:00 @zippera 为什么要urlretrieve呢?(我只知道抓图片用这个。。) 我的意思是,你直接用 url = 'http://mlook.mobi/book/info/6248' res = urllib2.urlopen(url) con=res.read() fhtml=open("con.html","w+") print >>fhtml,s 试试看。 | 
|      15mengzhuo      2013-08-15 22:53:28 +08:00 python开发必须设置系统级的编码utf8啊 ------------- 查询次数: 6 次,查询用时:70.36 ms 这是php? | 
|      16pandada8      2013-08-15 22:57:19 +08:00 via Android 先在本地看看编码,decode时候可以加个 error=“ignore"(直觉告诉我似乎我有拼写错误……参阅官方文档) | 
|  |      17VYSE      2013-08-15 23:03:56 +08:00 export LANG=en_US.UTF-8 中文系统上cmd里这样写肯定不是乱码: print res.read().decode('utf-8') 那就是CYGWIN输出编码的问题,不信你可以cat一个utf-8的html文件 | 
|  |      18zippera OP | 
|  |      19powerfj      2013-08-16 18:42:25 +08:00 请用linux.. | 
|  |      21infinte      2013-08-16 23:32:17 +08:00 Python2 在控制台输出用的似乎是 WriteConsoleA/WriteConsoleOutputA 不支持 unicode 的 所以死了这条心,换 py3 吧 | 
|  |      22jinfan1009      2013-08-17 15:21:29 +08:00 不是py3就能解决这样的问题哦,其实你这是读取网站的源文件,要看网站本来采用的是什么编码,不过所有的编码都可以解码程unicode,这样就是一致的! | 
|  |      24zippera OP @jinfan1009 这个网页本身是utf8的,程序本身也没问题,问题出在了print,出在了终端和系统。用Linux正常。 | 
|  |      27infinte      2013-08-17 18:32:14 +08:00 |