首页 > 代码库 > BeautifulSoup解决中文网页乱码

BeautifulSoup解决中文网页乱码

以下代码,在执行结果中的中文出现乱码。

from bs4 import BeautifulSoupimport urllib2request = urllib2.Request(http://www.163.com)response = urllib2.urlopen(request)html_doc = response.read()soup = BeautifulSoup(html_doc)print soup.find_all(a)

因为中文页面编码是gb2312,gbk,在BeautifulSoup构造器中传入from_encoding = "gb18030"参数可解决乱码问题。

注:在BeautifulSoup3中,from_encoding需修改为fromEncoding。

from bs4 import BeautifulSoupimport urllib2request = urllib2.Request(http://www.163.com)response = urllib2.urlopen(request)html_doc = response.read()soup = BeautifulSoup(html_doc, from_encoding = "gb18030")print soup.find_all(a)

 

BeautifulSoup解决中文网页乱码