def clean_html(buf):
 buf = buf.strip()
 if (not buf):
  return buf
 html_parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder('dom'), tokenizer=HTMLSanitizer)
 dom_tree = html_parser.parseFragment(buf)
 walker = treewalkers.getTreeWalker('dom')
 stream = walker(dom_tree)
 s = serializer.htmlserializer.HTMLSerializer(omit_optional_tags=False, quote_attr_values=True)
 output = s.render(stream, 'utf-8')
 while ('toberemoved' in output):
  oldoutput = output
  matches = re.findall('&lt;toberemoved.*?&gt;.*?&lt;/toberemoved&gt;', output, re.DOTALL)
  for s in matches:
   output = output.replace(s, '')
  matches = re.findall('&lt;/toberemoved&gt;', output, re.DOTALL)
  for s in matches:
   output = output.replace(s, '')
  matches = re.findall('&lt;toberemoved.*?&gt;', output, re.DOTALL)
  for s in matches:
   output = output.replace(s, '')
  if (output == oldoutput):
   break
 return output

def clean_html ( buf ) : buf = buf . strip ( ) if ( not buf ) : return buf html_parser = html5lib . HTMLParser ( tree = treebuilders . getTreeBuilder ( 'dom' ) , tokenizer = HTMLSanitizer ) dom_tree = html_parser . parseFragment ( buf ) walker = treewalkers . getTreeWalker ( 'dom' ) stream = walker ( dom_tree ) s = serializer . htmlserializer . HTMLSerializer ( omit_optional_tags = False , quote_attr_values = True ) output = s . render ( stream , 'utf-8' ) while ( 'toberemoved' in output ) : oldoutput = output matches = re . findall ( '&lt;toberemoved.*?&gt;.*?&lt;/toberemoved&gt;' , output , re . DOTALL ) for s in matches : output = output . replace ( s , '' ) matches = re . findall ( '&lt;/toberemoved&gt;' , output , re . DOTALL ) for s in matches : output = output . replace ( s , '' ) matches = re . findall ( '&lt;toberemoved.*?&gt;' , output , re . DOTALL ) for s in matches : output = output . replace ( s , '' ) if ( output == oldoutput ) : break return output
remove html markup from the given string .