使用Python Beautifulsoup获取特定的NHL分数
我试图只为一个特定的团队取得总分。我写了以下内容:使用Python Beautifulsoup获取特定的NHL分数,python,html,web-scraping,beautifulsoup,html-parsing,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 sc
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&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&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&type=stats">Vancouver</a></td>]
[最终版本]
1.
2.
3.
托特
1.
2.
1.
4.
0
2.
1.
3.
, ]
我似乎找不到中间块的
4
中的4。如果只可能得到1 2 1 4值,我可以比较这些值并总是选择最大的,但我似乎都走不了那么远。提前感谢。找到包含温哥华
的标签,然后使用以下方法获取下一个td
标签:
印刷品:
1
2
1
4
谢谢,正是我需要的。
1
2
1
4