Strip tags and keep content with Beautifulsoup

Question:

I thought this question would have been answered 1000 times, but apparently not (or I’m not looking right!). I want to clean up some overloaded HTML content with BeautifulSoup and remove unwanted tags. In some cases (e.g. <span> or <div>), I want to preserve the content of the tag instead of destroying it entirely with decompose.

With LXML, this can be achieved with strip_tag. How do I do that with BS4?

Asked By: mrgou

||

Answers:

Check out unwrap method. It preserves content.

Answered By: Adelina
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.