Reputation: 209
If I have the follow html structure, how do I print just the "print this" text?
<div class="a">
<div>
<strong>
Skip this
</strong>
<span>
skip this
</span>
</div>
print this
</div>
Thanks
Upvotes: 0
Views: 2230
Reputation: 215047
You might use contents for this;
from bs4 import BeautifulSoup
soup = BeautifulSoup("""<div class="a">
<div>
<strong>
Skip this
</strong>
<span>
skip this
</span>
</div>
print this
</div>""")
# the text you need is the last element of the contents
soup.find('div', {'class': 'a'}).contents[-1].strip()
# u'print this'
Upvotes: 1