Skip to content
Advertisement

Beautiful Soup 4: Remove comment tag and its content

The page that I’m scraping contains these HTML codes. How do I remove the comment tag <!-- --> along with its content with bs4?

JavaScript

Advertisement

Answer

You can use extract() (solution is based on this answer):

PageElement.extract() removes a tag or string from the tree. It returns the tag or string that was extracted.

JavaScript

As a result you get your div without comments:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement