我想从下面的代码中的地址读取一些utf-8文件。它适用于其中的大部分,但对于某些文件,urllib2(和urllib)无法读取。来自urlopen的垃圾
这里很明显的答案是第二个文件已损坏,但奇怪的是IE浏览器都读取了它们,而且完全没有问题。代码已经在XP和Linux上进行了测试,结果相同。任何消化?
import urllib2
#This works:
f=urllib2.urlopen("http://www.gutenberg.org/cache/epub/145/pg145.txt")
line=f.readline()
print "this works: %s)" %(line)
line=unicode(line,'utf-8') #... works fine
#This doesn't
f=urllib2.urlopen("http://www.gutenberg.org/cache/epub/144/pg144.txt")
line=f.readline()
print "this doesn't: %s)" %(line)
line=unicode(line,'utf-8')#...causes an exception:
非常感谢,并感谢您的链接! – user1023380