gpt4 book ai didi

python - 我不太明白如何解析 Yahoo NHL 页面

转载 作者:太空宇宙 更新时间:2023-11-03 18:51:46 24 4
gpt4 key购买 nike

这是迄今为止我的代码:

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)

我遇到的问题是该 yahoo 页面的 HTML 具有以下上下文中的表数据:<td class="yspscores">

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

最佳答案

您抓取了第一个表格,但该页面上有多个表格。事实上,有 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_ 关键字参数完成的。

关于python - 我不太明白如何解析 Yahoo NHL 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18196894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com