Reputation: 12500
A lot of questions here with similar title but I'm trying to remove the tag from the soup object itself.
I have a page that contains among other things this div
:
<div id="content">
I want to keep this<br /><div id="blah">I want to remove this</div>
</div>
I can select <div id="content">
with soup.find('div', id='content')
but I want to remove the <div id="blah">
from it.
Upvotes: 11
Views: 18930
Reputation: 879083
The Tag.decompose
method removes tag
from the tree.
So find the div
tag:
div = soup.find('div', {'id':'content'})
Loop over all the children but the first:
for child in list(div)[1:]:
and try to decompose the children:
try:
child.decompose()
except AttributeError: pass
import bs4 as bs
content = '''<div id="content">
I want to keep this<br /><div id="blah">I want to remove this</div>
</div>'''
soup = bs.BeautifulSoup(content)
div = soup.find('div', {'id':'content'})
for child in list(div)[1:]:
try:
child.decompose()
except AttributeError: pass
print(div)
yields
<div id="content">
I want to keep this
</div>
The equivalent using lxml would be
import lxml.html as LH
content = '''<div id="content">
I want to keep this<br /><div id="blah">I want to remove this</div>
</div>'''
root = LH.fromstring(content)
div = root.xpath('//div[@id="content"]')[0]
for child in div:
div.remove(child)
print(LH.tostring(div))
Upvotes: 8
Reputation: 61225
You can use extract
if you want to remove a tag or string from the tree.
In [13]: soup = BeautifulSoup("""<div id="content">
I want to keep this<br /><div id="blah">I want to remove this</div>
</div>""")
In [14]: soup = BeautifulSoup("""<div id="content">
....: I want to keep this<br /><div id="blah">I want to remove this</div>
....: </div>""")
In [15]: blah = soup.find(id='blah')
In [16]: _ = blah.extract()
In [17]: soup
Out[17]:
<html><body><div id="content">
I want to keep this<br/>
</div></body></html>
Upvotes: 12