使用Python 2.7.6中的bs4解析此示例文档:
<html>
<body>
<p>HTML allows omitting P end-tags.
<p>Like that and this.
<p>And this, too.
<p>What happened?</p>
<p>And can we <p>nest a paragraph, too?</p></p>
</body>
</html>
使用:
from bs4 import BeautifulSoup as BS
...
tree = BS(fh)
长期以来,HTML允许省略各种元素类型的结束标记,包括P标签(请检查模式或解析器)。然而,bs4对于这个文档的prettify()函数显示,在遇到</body>标签之前,它不会结束任何一个段落:
<html>
<body>
<p>
HTML allows omitting P end-tags.
<p>
Like that and this.
<p>
And this, too.
<p>
What happened?
</p>
<p>
And can we
<p>
nest a paragraph, too?
</p>
</p>
</p>
</p>
</p>
</body>
并不是 prettify() 的问题,因为手动遍历树时得到的结构相同:
<[document]>
<html>
␊
<body>
␊
<p>
HTML allows omitting P end-tags.␊␊
<p>
Like that and this.␊␊
<p>
And this, too.␊␊
<p>
What happened?
</p>
␊
<p>
And can we
<p>
nest a paragraph, too?
</p>
</p>
␊
</p>
</p>
</p>
</body>
␊
</html>
␊
</[document]>
现在,这将是XML的正确结果(至少到</body>,此时应报告WF错误)。但这不是XML。为什么?