Q:

Handle HTML tags using python

in a HTML-file, I've got the following occurrences:
<span class="finereader"></span>

or 
<span class="finereader">a</span>

I'd like to remove all these tags. The second example shows that it's possible that there is a letter (or number, but only 1) under the tag. The letter shouldn't be removed, only <span class="finereader"> and the following </span>.
Is there any re.sub-expression which can do this?
Thanks for any help.

A:

Another solution using BeautifulSoup:
from bs4 import BeautifulSoup

soup = BeautifulSoup(open('htmlfile'))

for elem in soup.find_all('span', class_='finereader'):
    elem.replace_with(elem.string or '') 

print(soup.prettify())

