gpt4 book ai didi

python - 剥离标签 python

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

我想要以下功能。

input : this is test <b> bold text </b> normal text
expected output: this is test normal text

即移除指定标签的内容

最佳答案

使用 BeautifulSoup 的解决方案:

from BeautifulSoup import BeautifulSoup
def removeTag(soup, tagname):
for tag in soup.findAll(tagname):
contents = tag.contents
parent = tag.parent
tag.extract()

s = BeautifulSoup("abcd <b> btag </b> hello <d>dtag</d>")

removeTag(s,"b")
print s
removeTag(s, "d")
print s

返回:

>>>
abcd hello <d>dtag</d>
abcd hello

关于python - 剥离标签 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2584885/

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