def endtags(html):
 NON_CLOSING_TAGS = ['AREA', 'BASE', 'BASEFONT', 'BR', 'COL', 'FRAME', 'HR', 'IMG', 'INPUT', 'ISINDEX', 'LINK', 'META', 'PARAM']
 opened_tags = re.findall('<([a-z]+)[^<>]*>', html)
 closed_tags = re.findall('</([a-z]+)>', html)
 opened_tags = [i.lower() for i in opened_tags if (i.upper() not in NON_CLOSING_TAGS)]
 closed_tags = [i.lower() for i in closed_tags]
 len_opened = len(opened_tags)
 if (len_opened == len(closed_tags)):
  return html
 opened_tags.reverse()
 for tag in opened_tags:
  if (tag in closed_tags):
   closed_tags.remove(tag)
  else:
   html += ('</%s>' % tag)
 return html

def endtags ( html ) : NON_CLOSING_TAGS = [ 'AREA' , 'BASE' , 'BASEFONT' , 'BR' , 'COL' , 'FRAME' , 'HR' , 'IMG' , 'INPUT' , 'ISINDEX' , 'LINK' , 'META' , 'PARAM' ] opened_tags = re . findall ( '<([a-z]+)[^<>]*>' , html ) closed_tags = re . findall ( '</([a-z]+)>' , html ) opened_tags = [ i . lower ( ) for i in opened_tags if ( i . upper ( ) not in NON_CLOSING_TAGS ) ] closed_tags = [ i . lower ( ) for i in closed_tags ] len_opened = len ( opened_tags ) if ( len_opened == len ( closed_tags ) ) : return html opened_tags . reverse ( ) for tag in opened_tags : if ( tag in closed_tags ) : closed_tags . remove ( tag ) else : html += ( '</%s>' % tag ) return html
close all open html tags at the end of the string .