我不太明白如何解析Yahoo NHL Page

时间:2013-08-12 21:21:05

标签: python python-3.x beautifulsoup

到目前为止,这是我的代码:

from bs4 import BeautifulSoup
from urllib.request import urlopen

url = urlopen("http://sports.yahoo.com/nhl/scoreboard?d=2013-04-01")

content = url.read()

soup = BeautifulSoup(content)

print (soup.prettify)

table = soup.find('table')
rows = table.findAll('tr')

for tr in rows:
    cols = tr.findAll('td')
    for td in cols:
        text = td.findAll('yspscores')
        for yspscores in td:
            print (yspscores)

我遇到的问题是该雅虎页面的HTML在此上下文中包含表格数据:<td class="yspscores">

我不太明白如何在我的代码中引用它。我的目标是打印出乐谱所对应的乐队的分数和名称。

1 个答案:

答案 0 :(得分:1)

您抓住了第一个表,但该页面上有多个表。实际上,有 46 表。

您想要找到包含scores类的表:

for table in soup.find_all('table', class_='scores'):
    for row in table.find_all('tr'):
        for cell in row.find_all('td', class_='yspscores'):
            print(cell.text)

请注意,使用class_关键字参数搜索特定类。