使用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