这个问题特定于BeautifulSoup4,这使得它与以前的问题不同:
BeautifulSoup中的selfClosingTags
既然BeautifulStoneSoup已经消失了(以前的xml解析器),我怎样才能bs4尊重新的自闭标签?例如:
import bs4
S = '''<foo> <bar a="3"/> </foo>'''
soup = bs4.BeautifulSoup(S, selfClosingTags=['bar'])
print soup.prettify()
Run Code Online (Sandbox Code Playgroud)
不会自动关闭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>
Run Code Online (Sandbox Code Playgroud)