user2333196
user2333196

Reputation: 5776

extracting data from div tags Python

I am trying to scrape data from a webpage that has some of the data nested in div tags.

url = 'http://london2012.fiba.com/pages/eng/fe/12/olym/p/gid/26/grid/A/rid/9087/sid/6233/game.html'
boxurl = urllib2.urlopen(url).read()
soup = BeautifulSoup(boxurl)

linescoreA = soup.find("div", {"class": "scoreA"})

print linescoreA

outputs this:

<div class="scoreA">
<div class="period">19</div>
<div class="period">22</div>
<div class="period">22</div><div class="period">26</div>
<div class="final">89</div>
<div class="clear"></div>
</div>

and that is where I get stuck. How do I get the data from the div tags?

Upvotes: 1

Views: 1638

Answers (2)

kiriloff
kiriloff

Reputation: 26333

Try

for node in soup.find("div", {"class": "scoreA"}):
    print ''.join(node.findAll(text=True))

and what about

for node in soup.find("div", {"class": "scoreA"}):
        print node.string

I am sorry, i cannot try for you.

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121524

To get just the textual data, use .stripped_strings:

print list(linescoreA.stripped_strings)

Upvotes: 3

Related Questions