问题描述
我想用另一个标签替换一个标签,并将旧标签的内容放在新标签之前.例如:
I want to replace a tag with another tag and put the contents of the old tag before the new one. For example:
我要更改此内容
<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()
,但是如何用文本和替换标签或在替换的标签之前插入文本?
I can easily find all spans
with find_all()
, get the number from the id attribute and replace one tag with another tag using replace_with()
, but how do I replace a tag with text and a new tag or insert text before a replaced tag?
推荐答案
想法是找到每个具有 id
属性( span [id]
CSS选择器),请使用 insert_after()
在其后插入 sup
标记,然后 unwrap()
将标签替换为其内容:
The idea is to find every span
tag with id
attribute (span[id]
CSS Selector), use insert_after()
to insert a sup
tag after it and unwrap()
to replace the tag with it's contents:
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>
这篇关于Beautiful Soup 4:如何用文本和其他标签替换标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!