从nhl.com导入并从表中选择数据

2024-10-03 02:44:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的初学者,我刚刚开始了一个暂时搁置的项目,基本上,我需要从nhl.com上的表导入数据

我有我的旧代码(大约1.5年),但显示和页面的一般格式已经改变,不再工作了

我工作的那张桌子是关于家里的Powerplay的

The URL is: http://www.nhl.com/stats/teams?report=powerplay&reportType=season&seasonFrom=20202021&seasonTo=20202021&gameType=2&homeRoad=H&filter=gamesPlayed,gte,1&sort=powerPlayPct&page=0&pageSize=50

The API (if I got it correctly is): https://api.nhle.com/stats/rest/en/team/powerplay?isAggregate=false&isGame=false&sort=%5B%7B%22property%22:%22powerPlayPct%22,%22direction%22:%22DESC%22%7D%5D&start=0&limit=50&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20homeRoad=%22H%22%20and%20seasonId%3C=20202021%20and%20seasonId%3E=20202021

我想提取的是:

1) Team
2) PP%

(第一列和最后一列之前)

我知道这对这里的大多数人来说都很容易,但我已经被困了一段时间了,任何形式的帮助都将不胜感激

先走一步


Tags: the数据项目代码comfalseis格式
1条回答
网友
1楼 · 发布于 2024-10-03 02:44:53

您可以使用以下示例从API URL加载数据:

import requests


url = 'https://api.nhle.com/stats/rest/en/team/powerplay?isAggregate=false&isGame=false&sort=%5B%7B%22property%22:%22powerPlayPct%22,%22direction%22:%22DESC%22%7D%5D&start=0&limit=50&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20homeRoad=%22H%22%20and%20seasonId%3C=20202021%20and%20seasonId%3E=20202021'
data = requests.get(url).json()

for i in data['data']:
    print('{:<30} {:.2f}'.format(i['teamFullName'], i['powerPlayPct']*100))

印刷品:

Washington Capitals            45.16
Carolina Hurricanes            38.64
Boston Bruins                  38.46
Chicago Blackhawks             33.33
New York Islanders             30.30
Winnipeg Jets                  28.95
Dallas Stars                   28.85
Los Angeles Kings              28.57
Edmonton Oilers                28.12
Pittsburgh Penguins            25.81
Toronto Maple Leafs            25.58
Buffalo Sabres                 24.39
Vancouver Canucks              23.64
Calgary Flames                 23.25
Colorado Avalanche             22.45
Tampa Bay Lightning            21.62
Florida Panthers               21.62
Montréal Canadiens             21.43
San Jose Sharks                21.43
Ottawa Senators                20.93
Columbus Blue Jackets          20.00
Philadelphia Flyers            19.15
Vegas Golden Knights           17.95
Arizona Coyotes                16.07
St. Louis Blues                13.64
Nashville Predators            13.33
New York Rangers               12.24
New Jersey Devils              8.57
Minnesota Wild                 6.06
Detroit Red Wings              5.56
Anaheim Ducks                  4.76

相关问题 更多 >