John Smith
John Smith

Reputation: 209

How to skip tags with beautifulsoup

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

Answers (1)

akuiper
akuiper

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

Related Questions