简体   繁体   English

如何使用BeautifulSoup将标签替换为其内容?

[英]How do I use BeautifulSoup to replace a tag with its contents?

How would I use BeautifulSoup to remove only a tag? 我如何使用BeautifulSoup 删除标签? The method I found deletes the tag and all other tags and content inside it. 我发现的方法删除了标签以及其中的所有其他标签和内容。 I want to remove only the tag and leave everything inside it untouched, eg 我想只删除标签,并将其中的所有内容保持不变,例如

change this: 改变这个:

<div>
<p>dvgbkfbnfd</p>
<div>
<span>dsvdfvd</span>
</div>
<p>fvjdfnvjundf</p>
</div>

to this: 对此:

<p>dvgbkfbnfd</p>
<span>dsvdfvd</span>
<p>fvjdfnvjundf</p>

I've voted to close as a duplicate, but in case it's of use, reapplying slacy's answer from top related answer on the right gives you this solution: 我已经投票结束了复制,但如果它有用,重新应用slacy的答案从右边的相关答案给你这个解决方案:

from BeautifulSoup import BeautifulSoup

html = '''
<div>
<p>dvgbkfbnfd</p>
<div>
<span>dsvdfvd</span>
</div>
<p>fvjdfnvjundf</p>
</div>
'''

soup = BeautifulSoup(html)
for match in soup.findAll('div'):
    match.replaceWithChildren()

print soup

... which produces the output: ...产生输出:

<p>dvgbkfbnfd</p>

<span>dsvdfvd</span>

<p>fvjdfnvjundf</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM