I have parsed html page: using beautifulsoup
user_page = urllib2.urlopen(user_url)
souping_page = bs(user_page)
badges = souping_page.body.find('div', attrs={'class': 'badges'})
after this my badges
object looks like this:
<span><span title="9 gold badges"><span class="badge1"></span><span class="badgecount">9</span></span><span title="38 silver badges"><span class="badge2"></span><span class="badgecount">38</span></span><span title="56 bronze badges"><span class="badge3"></span><span class="badgecount">56</span></span></span>
Now I want to extract example 9 gold badges
, 38 silver badges
from this, I tried to use badges.span.span
but that doesn't work.
Get the parent span
from badges
, find all top-level spans inside by using find_all()
with recursive=False
:
from bs4 import BeautifulSoup
page = """<div class="badges">
<span>
<span title="9 gold badges"><span class="badge1"></span><span class="badgecount">9</span></span>
<span title="38 silver badges"><span class="badge2"></span><span class="badgecount">38</span></span>
<span title="56 bronze badges"><span class="badge3"></span><span class="badgecount">56</span></span>
</span>
</div>"""
soup = BeautifulSoup(page)
badges = soup.body.find('div', attrs={'class': 'badges'})
for span in badges.span.find_all('span', recursive=False):
print span.attrs['title']
prints:
9 gold badges
38 silver badges
56 bronze badges
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With