相关疑难解决方法(0)

如何让BeautifulSoup 4尊重自动关闭标签?

这个问题特定于BeautifulSoup4,这使得它与以前的问题不同:

为什么BeautifulSoup会修改我的自闭元素?

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)

python xml beautifulsoup xml-parsing

8
推荐指数
1
解决办法
3672
查看次数

标签 统计

beautifulsoup ×1

python ×1

xml ×1

xml-parsing ×1