def strip_html_comments(html): soup = BeautifulSoup(html, 'html.parser') for element in soup.find_all(text=lambda text: isinstance(text, html_comment)): element.extract() return str(soup)