gpt4 book ai didi

python - BeautifulSoup 4 : How to replace a tag with text and another tag?

转载 作者:太空狗 更新时间:2023-10-29 14:14:28 24 4
gpt4 key购买 nike

我想用另一个标签替换一个标签,并将旧标签的内容放在新标签之前。例如:

我想改变这个:

<html>
<body>
<p>This is the <span id="1">first</span> paragraph</p>
<p>This is the <span id="2">second</span> paragraph</p>
</body>
</html>

进入这个:

<html>
<body>
<p>This is the first<sup>1</sup> paragraph</p>
<p>This is the second<sup>2</sup> paragraph</p>
</body>
</html>

我可以使用 find_all() 轻松找到所有 spans,从 id 属性中获取数字并使用 replace_with()< 将一个标签替换为另一个标签,但如何用文本 替换标签或在替换标签前插入文本?

最佳答案

想法是找到每个带有 id 属性的 span 标签 (span[id] CSS Selector ),使用 insert_after()在它和 unwrap() 之后插入一个 sup 标签用它的内容替换标签:

from bs4 import BeautifulSoup

data = """
<html>
<body>
<p>This is the <span id="1">first</span> paragraph</p>
<p>This is the <span id="2">second</span> paragraph</p>
</body>
</html>
"""

soup = BeautifulSoup(data)
for span in soup.select('span[id]'):
# insert sup tag after the span
sup = soup.new_tag('sup')
sup.string = span['id']
span.insert_after(sup)

# replace the span tag with it's contents
span.unwrap()

print soup

打印:

<html>
<body>
<p>This is the first<sup>1</sup> paragraph</p>
<p>This is the second<sup>2</sup> paragraph</p>
</body>
</html>

关于python - BeautifulSoup 4 : How to replace a tag with text and another tag?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27006463/

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