使用mechanize
,我检索到一个网页的源页面,其中包含一些非ASCII字符,如中文字符。python编码
代码低于:
#using python2.6
from mechanize import Browser
br = Browser()
br.open("http://www.example.html")
src = br.reponse().read() #retrieve the source of the web
print src #print the src
问:
1。根据该页面的源代码,我可以看到,它的charset=gb2312
,但是当我print src
,所有的内容是正确的,我的意思是没有胡言乱语。为什么? print
知道src的编码吗?
2.我应该明确解码还是编码src?
打印根据控制台的编码方案为您编码。如果你想输出结果到文件,你需要对它进行编码 – xiaohan2012