for e in soup.find_all():
    for x in e.children:
        if isinstance(x, element.Comment):
            x.replace_with('')
for e in soup.find_all():
    if not len(no_nl(e.text).strip()):
        e.extract()
for s in str(soup).split('\n'):
    if len(s.strip()):
        pass
Removing empty nodes from HTML