i want the following functionality.
input : this is test <b> bold text </b> normal text
expected output: this is test normal text
i.e. remove the content of the specified tag
Solution using 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
returns:
>>>
abcd hello <d>dtag</d>
abcd hello
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With