c.j.mcdonn
c.j.mcdonn

Reputation: 1235

Scrape specific NHL score with Python Beautifulsoup

I am trying to scrape only the total score for a specified team. I have written the following:

import urllib.request
import re
from bs4 import BeautifulSoup

#url1 = "http://scores.nbcsports.com/nhl/scoreboard.asp"

## This works, however is using a set day for testing, will need url changed to url1 for current day scoreboard
url = "http://scores.nbcsports.com/nhl/scoreboard.asp?day=20141202"
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page)

allrows = soup.findAll('td')
userows = [t for t in allrows if t.findAll(text=re.compile('Vancouver'))]

print(userows)

This returns:

[<td><table cellspacing="0"><tr class="shsTableTtlRow"><td class="shsNamD" colspan="1">Final</td>
<td class="shsTotD">1</td>
<td class="shsTotD">2</td>
<td class="shsTotD">3</td>
<td class="shsTotD">Tot</td>
</tr>
<tr>
<td class="shsNamD" nowrap=""><span class="shsLogo"><span class="shsNHLteam22sm_trans"></span></span><a href="/nhl/teamstats.asp?teamno=22&amp;type=stats">Vancouver</a></td>
<td class="shsTotD">1</td>
<td class="shsTotD">2</td>
<td class="shsTotD">1</td>
<td class="shsTotD">4</td>
</tr>
<tr>
<td class="shsNamD" nowrap=""><span class="shsLogo"><span class="shsNHLteam23sm_trans"></span></span><a href="/nhl/teamstats.asp?teamno=23&amp;type=stats">Washington</a></td>
<td class="shsTotD">0</td>
<td class="shsTotD">2</td>
<td class="shsTotD">1</td>
<td class="shsTotD">3</td>
</tr>
</table>
</td>, <td class="shsNamD" nowrap=""><span class="shsLogo"><span class="shsNHLteam22sm_trans"></span></span><a href="/nhl/teamstats.asp?teamno=22&amp;type=stats">Vancouver</a></td>]

What I can't seem to get to is the 4 in <td class="shsTotD">4</td> from the middle block. If it is only possible to get the 1 2 1 4 I could compare the values and always pick the largest, but I can't even seem to get that far. Thanks in advance.

Upvotes: 2

Views: 922

Answers (1)

alecxe
alecxe

Reputation: 474001

Find the tag containing Vancouver and get the next td tags by using find_next_siblings():

vancouver = soup.find('a', text='Vancouver')
for td in vancouver.parent.find_next_siblings('td', class_='shsTotD'):
    print(td.text)

Prints:

1
2
1
4

Upvotes: 1

Related Questions