Mar-24-2020, 05:13 PM
Hello! I am currently working on a project relating to retrieving data from the NHL API and have been able to pull from a single team but I can't figure out how to retrieve player data without first retrieving the roster data to get the player id and link. right now I pull an individual team roster based on their API link to get player IDs but I want to be able to query player directly on different teams without possibly having to select the roster first
roster link: http://statsapi.web.nhl.com/api/v1/teams/1/roster
example player link: http://statsapi.web.nhl.com/api/v1/peopl...gleSeason)
roster link: http://statsapi.web.nhl.com/api/v1/teams/1/roster
example player link: http://statsapi.web.nhl.com/api/v1/peopl...gleSeason)
import json from urllib.request import urlopen url_roster = 'http://statsapi.web.nhl.com/api/v1/teams/1/roster' Cap_Player_Name = input() Player_Name = Cap_Player_Name.title() NHL_url = 'http://statsapi.web.nhl.com' url_extension = '?hydrate=stats(splits=statsSingleSeason)' with urlopen(url_roster) as response: source = response.read() data = json.loads(source) for item in data['roster']: if item['person']['fullName'] == Player_Name: if item['position']['code'] == 'G': goalie_code = (item['person']['link']) url_full = NHL_url + str(goalie_code) + url_extension with urlopen(url_full) as response: source = response.read() data = json.loads(source) for item5 in data['people']: print('') print(item5['fullName']) for item2 in item5['stats']: for item3 in item2['splits']: print('') print('wins: ', item3['stat']['wins']) print('losses: ', item3['stat']['losses']) print('saves: ', item3['stat']['saves']) print('goalAgainstAverage: ', item3['stat']['goalAgainstAverage'], '%') elif item['position']['code'] == 'D' or 'C' or 'L' or 'R': player_code = (item['person']['link']) url_full = NHL_url + str(player_code) + url_extension with urlopen(url_full) as response: source = response.read() data = json.loads(source) for item5 in data['people']: print(item5['fullName']) print('') for item2 in item5['stats']: for item3 in item2['splits']: shots = item3['stat']['shots'] assists = item3['stat']['assists'] goals = item3['stat']['goals'] hits = item3['stat']['hits'] blocks = item3['stat']['blocked'] points = item3['stat']['points'] plusMinus = item3['stat']['plusMinus'] print('') print('Shots: ', shots) print('Assists: ', assists) print('Goals: ', goals) print('Hits: ', hits) print('Blocks: ', blocks) print('Points: ', points) print('Plus/Minus: ', plusMinus)