code: def clean_html5lib(input):
      
      from html5lib import treebuilders, treewalkers, serializer, sanitizer
  
      p = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("dom"))
      dom_tree = p.parseFragment(input)
      walker = treewalkers.getTreeWalker("dom")
      stream = walker(dom_tree)
  
      s = serializer.htmlserializer.HTMLSerializer(omit_optional_tags=False)
      return "".join(s.serialize(stream))
docs: Takes an HTML fragment and processes it using html5lib to ensure that the HTML is well-formed.
  
      >>> clean_html5lib("<p>Foo<b>bar</b></p>")
      u'<p>Foo<b>bar</b></p>'
      >>> clean_html5lib("<p>Foo<b>bar</b><i>Ooops!</p>")
      u'<p>Foo<b>bar</b><i>Ooops!</i></p>'
      >>> clean_html5lib('<p>Foo<b>bar</b>& oops<a href="#foo&bar">This is a <>link</a></p>')
      u'<p>Foo<b>bar</b>&amp; oops<a href=#foo&amp;bar>This is a &lt;&gt;link</a></p>'