extract, decompose and clear
Three methods remove content from the tree, with different consequences: extract() lifts a node out but keeps it usable in Python, decompose() removes it and lets the garbage collector reclaim it, and clear() empties a tag's contents in place.
.extract() - Remove but Keep
from bs4 import BeautifulSoup
soup = BeautifulSoup("<div><p>a</p><p>b</p></div>", "html.parser")
p = soup.find("p")
removed = p.extract()
print(soup) # <div><p>b</p></div>
print(removed) # <p>a</p> - still a valid object
print(removed.parent) # None
Use extract() when you want the removed node for later re-insertion or separate processing.
.decompose() - Remove and Forget
soup = BeautifulSoup("<div><script>var x=1;</script><p>a</p></div>", "html.parser")
soup.script.decompose()
print(soup) # <div><p>a</p></div>
decompose() is the right tool for noise you never want again: scripts, styles, comments, ad containers. After decompose() the object is no longer usable.
.clear() - Empty a Tag
soup = BeautifulSoup("<p><b>x</b> y</p>", "html.parser")
soup.p.clear()
print(soup) # <p/>
clear() removes all children of a tag but keeps the tag itself, which is handy before filling it with new content.
Choosing the Right Method
- Remove and reuse the node: extract().
- Remove noise permanently: decompose().
- Empty a container in place: clear().
Summary
extract() removes a node from the tree while keeping it as a usable object, decompose() removes it permanently (ideal for script and style cleanup), and clear() empties a tag's contents while keeping the tag. Pick by whether you need the removed content afterward.