代码如下:
def find(self):
HtmlData = self.Data() #urllib获取的结果(页面编码gb2312)
HtmlSoup = BeautifulSoup(HtmlData,"lxml")
FindALL = HtmlSoup.find_all('a')
# print HtmlSoup.encode("gb2312") #能够正常打印
print str(FindALL).encode("gb2312") #出现乱码
抓取的页面编码是gb2312,打印的时候使用encode能够正常输出中文字符,但是使用find_all的时候打印出来是乱码
如下:
et="_blank">\u4e2d\u534e\u6c11\u4fd7\u793c\u4eea</a>, <a href="Doc/1603/2520447.
htm" target="_blank">\u6e05\u660e\u8282\u626b\u5893\uff0c\u90fd\u6709\u90a3\u4e9
查了发现bs默认编码是utf-8,所以会出现这样的问题,由于本人菜鸡一枚,查了好多都没弄懂。
网上大牛都说python2编码水很深,现在碰到这个问题真心头疼 。请教大牛有什么好的办法对编码进行转换?或者让它可以正常输出中文字符?
PHPz2017-04-17 17:49:46
Your FindALL here is a list. If you want to lose, it should be output in a loop
for _ in FindALL:
print _.encode('utf-8')
or
print json.dumps(FindALL, ensure_ascii=False)
PHP中文网2017-04-17 17:49:46
According to the answer above, Chinese characters were successfully printed:
def find(self):
HtmlData = self.Data()
HtmlSoup = BeautifulSoup(HtmlData,"lxml")
FindALL = HtmlSoup.find_all('a')
for i in FindALL:
print i.encode('gb2312')
#输出:
#<a href="/today/todayxx-1843.htm" target="_blank">台湾归顺式场惨案发生(1902年)
#</a>
#<a href="/today/todayxx-1844.htm" target="_blank">墨西哥反叛分子获胜迪亚斯总统离
#职(1</a>
#<a href="/today/todayxx-1845.htm" target="_blank">首批美国军用飞机在法参战(1918
#年</a>
#<a href="/today/todayxx-1846.htm" target="_blank">中共决定将工农革命军定名“红军
#”(1</a>
Thank you very much!