Ravash Jalil
Ravash Jalil

Reputation: 820

Python - BeautifulSoup - Grab data enclosed Divs & Span's

I have been trying for a while now to grab the "$6.4K" from this piece of html with BS4, but it's been difficult since its location is a little tricky for me.

<div class="blk game">
<div class="blk away-team">
<div class="pitcher players">
<a href="http://rosl.tu" class="player-popup" data-url="http://rosl.tu">Jake Peavy</a>
<span class="meta stats">
<span class="stats"> R </span> 
$6.4K <span class="fpts" title="Projected Points" data-role="authorize" data-product="56">7.17</span>
</span>
</div>...

Upvotes: 0

Views: 164

Answers (1)

Brian
Brian

Reputation: 2242

There are lots of ways. The best way, I think, would be to pick something the least fragile - something that won't break if the HTML changes. That said, I don't know how consistent you can expect the HTML to be.

So, one way would be to just pick the 3rd child node of the <span> that has the "meta" tag. To do that, you could do:

from bs4 import BeautifulSoup
html = '''
<div class="blk game">
<div class="blk away-team">
<div class="pitcher players">
<a href="http://rosl.tu" class="player-popup" data-url="http://rosl.tu">Jake Peavy</a>
<span class="meta stats">
<span class="stats"> R </span> 
$6.4K <span class="fpts" title="Projected Points" data-role="authorize" data-product="56">7.17</span>
</span>
</div>
'''

soup = BeautifulSoup(html)
print(list(soup.find_all("span", class_="meta")[0].children)[2])

Which prints:

$6.4K

Upvotes: 2

Related Questions