gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:52:34 25 4
gpt4 key购买 nike

这是我的脚本:

import BeautifulSoup

if __name__ == "__main__":
data = """
<root>
<obj id="3"/>
<obj id="5"/>
<obj id="3"/>
</root>
"""
soup = BeautifulSoup.BeautifulStoneSoup(data)
print soup

运行时,打印:

<root>
<obj id="3"></obj>
<obj id="5"></obj>
<obj id="3"></obj>
</root>

我希望它保持相同的结构。我该怎么做?

最佳答案

来自Beautiful Soup documentation :

The most common shortcoming of BeautifulStoneSoup is that it doesn't know about self-closing tags. HTML has a fixed set of self-closing tags, but with XML it depends on what the DTD says. You can tell BeautifulStoneSoup that certain tags are self-closing by passing in their names as the selfClosingTags argument to the constructor

关于python - 为什么 BeautifulSoup 会修改我的自关闭元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1567402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com