Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strip tags python

Tags:

python

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

like image 227
developer Avatar asked Apr 06 '10 12:04

developer


1 Answers

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
like image 77
zoli2k Avatar answered Oct 17 '22 00:10

zoli2k