You can use extract() (solution based on this answer ):
PageElement.extract () removes the tag or line from the tree. Returns the tag or string that was retrieved.
from bs4 import BeautifulSoup, Comment data = """<div class="foo"> cat dog sheep goat <!-- <p>test</p> --> </div>""" soup = BeautifulSoup(data) div = soup.find('div', class_='foo') for element in div(text=lambda text: isinstance(text, Comment)): element.extract() print soup.prettify()
As a result, you will get your div without comment:
<div class="foo"> cat dog sheep goat </div>
alecxe
source share