【问题标题】:I do not quite understand how to parse the Yahoo NHL Page我不太明白如何解析 Yahoo NHL 页面
【发布时间】:2013-08-12 21:21:04
【问题描述】:

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

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">

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

【问题讨论】:

    标签: python python-3.x beautifulsoup


    【解决方案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_ 关键字参数完成的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      相关资源
      最近更新 更多