Python - urllib2.urlopen - 为什么会出现乱码?
这是我的问题:
import urllib2
response=urllib2.urlopen('http://proxy-heaven.blogspot.com/')
html=response.read()
print html
就是这个网站,我不知道为什么结果全是乱码。有人能帮忙吗?
3 个回答
0
编码可能是你的问题,这种情况下你需要用到这段代码。
import urllib
s = str(urllib.urlopen('http://proxy-heaven.blogspot.com/').read(), encoding='utf8')
1
对我来说是可以的:
import urllib
response=urllib.urlopen('http://proxy-heaven.blogspot.com/')
a = response.read()
print a[:50]
> '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Stric'
不过,你的终端可能有编码问题。
1
没有看到你的输出结果,很难判断具体问题,但我猜可能是编码的问题:这个网站使用的是utf8编码。如果你的终端设置成了iso-latin,比如说,那就可能无法正确显示字符。