How to get BeautifulSoup 4 to respect a self-closing tag?

前端 未结 1 434
挽巷
挽巷 2020-12-06 11:21

This question is specific to BeautifulSoup4, which makes it different from the previous questions:

Why is BeautifulSoup modifying my self-closing elements?

s

相关标签:
1条回答
  • 2020-12-06 11:58

    To parse XML you pass in “xml” as the second argument to the BeautifulSoup constructor.

    soup = bs4.BeautifulSoup(S, 'xml')
    

    You’ll need to have lxml installed.

    You don't need to pass selfClosingTags anymore:

    In [1]: import bs4
    In [2]: S = '''<foo> <bar a="3"/> </foo>'''
    In [3]: soup = bs4.BeautifulSoup(S, 'xml')
    In [4]: print soup.prettify()
    <?xml version="1.0" encoding="utf-8"?>
    <foo>
     <bar a="3"/>
    </foo>
    
    0 讨论(0)
提交回复
热议问题