import urllib2
from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen('http://www.leeon.me');
soup = BeautifulSoup(page,fromEncoding="gb18030")

print soup.originalEncoding
print soup.prettify()

如果中文页面编码是gb2312,gbk,在BeautifulSoup构造器中传入fromEncoding="gb18030"参数即可解决乱码问题,即使分析的页面是utf8的页面使用gb18030也不会出现乱码问题!

转自:http://leeon.me/a/beautifulsoup-chinese-page-resolve


Profeel
289 声望17 粉丝

Brilliant's everywhere.