我正在尝试识别并保存特定网站上的所有标题,但一直遇到编码错误。
该网站为:http://paper.people.com.cn/rmrb/html/2016-05/06/nw.D110000renmrb_20160506_2-01.htm
当前代码如下:
holder = {}
url = urllib.urlopen('http://paper.people.com.cn/rmrb/html/2016-05/06/nw.D110000renmrb_20160506_2-01.htm').read()
soup = BeautifulSoup(url, 'lxml')
head1 = soup.find_all(['h1','h2','h3'])
print head1
holder["key"] = head1
打印输出的结果是:
[<h3>\u73af\u5883\u6c61\u67d3\u6700\u5c0f\u5316 \u8d44\u6e90\u5229\u7528\u6700\u5927\u5316</h3>, <h1>\u5929\u6d25\u6ee8\u6d77\u65b0\u533a\uff1a\u697c\u5728\u666f\u4e2d \u5382\u5728\u7eff\u4e2d</h1>, <h2></h2>]
我相信那些是Unicode字符,但我还没有找到如何让Python将它们显示为字符的方法。
我已经尝试在其他地方寻找答案。更明确的问题是这个: Python和BeautifulSoup编码问题 建议添加。
soup = BeautifulSoup.BeautifulSoup(content.decode('utf-8','ignore'))
然而,这给了我与评论中提到的相同错误("AttributeError: type object 'BeautifulSoup' has no attribute 'BeautifulSoup'")。去掉第二个'.BeautifulSoup'会导致不同的错误("RuntimeError: maximum recursion depth exceeded while calling a Python object")。
我还尝试了这里建议的答案: Python中使用BeautifulSoup时出现中文字符编码错误? ,通过分解对象的创建。
html = urllib2.urlopen("http://www.515fa.com/che_1978.html")
content = html.read().decode('utf-8', 'ignore')
soup = BeautifulSoup(content)
但这也会产生递归错误。非常感谢您提供其他任何提示。谢谢。