【问题标题】:BeautifulSoup html5lib parsing strange phenomenon ..is that a bug?BeautifulSoup html5lib 解析奇怪现象..这是一个错误吗?
【发布时间】:2014-10-21 03:48:49
【问题描述】:

python2.6 + htmllib0.99 + bs4

运行以下代码时会抛出异常

#!/usr/bin/python
# -------_*_ coding: utf-8 _*_

from bs4 import BeautifulSoup
import html5lib    
html  = '''
<html>
<head>
<title> test
</title>
</head>
<body>
<div id="tcp">hello</div>
</body>
</html>
'''
cs = BeautifulSoup(html,"html5lib")
print cs.contents[0].contents[2].contents[1]['id']
main_tag = cs.find('div', id='tcp')

print main_tag.text


####result####

#tcp
#Traceback (most recent call last):
#  File "C:\Users\XXXXXXXX\Desktop\test.py", line 21, in <
#    print main_tag.text
#AttributeError: 'NoneType' object has no attribute 'text'

去掉“

”和“test”之间的空格后,程序运行成功

【问题讨论】:

    标签: beautifulsoup html5lib


    【解决方案1】:

    这是 bs4 中的一个已知错误。见:

    https://bugs.launchpad.net/beautifulsoup/+bug/1430633

    在某些情况下,bs4 会生成格式错误的树。然后“find”从树的末端跑出,并返回一个 None。

    【讨论】:

      猜你喜欢
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多