Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautifulsoup get span content

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.

like image 269
add-semi-colons Avatar asked Mar 07 '14 19:03

add-semi-colons


1 Answers

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.

like image 50
alecxe Avatar answered Sep 28 '22 04:09

alecxe