这个问题是BeautifulSoup4特有的,这使得它与前面的问题不同:
BeautifulSoup 中的 selfClosingTags
既然BeautifulStoneSoup
不见了(以前的 xml 解析器),我怎样才能bs4
尊重一个新的自闭标签?例如:
import bs4
S = '''<foo> <bar a="3"/> </foo>'''
soup = bs4.BeautifulSoup(S, selfClosingTags=['bar'])
print soup.prettify()
不会自动关闭bar
标签,但会给出提示。bs4 所指的这个树生成器是什么以及如何自我关闭标签?
/usr/local/lib/python2.7/dist-packages/bs4/__init__.py:112: UserWarning: BS4 does not respect the selfClosingTags argument to the BeautifulSoup constructor. The tree builder is responsible for understanding self-closing tags.
"BS4 does not respect the selfClosingTags argument to the "
<html>
<body>
<foo>
<bar a="3">
</bar>
</foo>
</body>
</html>