Destroying Tags With Beautiful Soup

There are various ways to edit tags with Beautiful Soup, The decompose method both removes the tag from the tree and destroys the object (along with all its descendants in the HTML tree).

from bs4 import BeautifulSoup
soup = BeautifulSoup(html)

element = soup.find(id=tag_id)

If you want to destroy tags around the one you found (along with the tag and its descendants in the tree) then call decompose on it's parent or parent's parent (and so on). Or figure out how to find its parent instead - whichever is easier.


After you edit the tree it might be a good idea to call smooth to clean up a bit.