使用Python Beautifulsoup刮取特定的NHL分数

时间:2014-12-04 20:39:44

标签: python html web-scraping beautifulsoup html-parsing

我试图仅抓取指定团队的总分。我写了以下内容:

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)

返回:

[<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>]

我似乎无法获得的是来自中间区块的<td class="shsTotD">4</td>中的 4 。如果只能获得 1 2 1 4 我可以比较这些值并总是选择最大值,但我似乎无法达到那么远。提前谢谢。

1 个答案:

答案 0 :(得分:1)

找到包含Vancouver的标记,并使用find_next_siblings()获取下一个td代码:

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

打印:

1
2
1
4