Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hockey Data Project
#1
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)

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)
Reply


Messages In This Thread
Hockey Data Project - by Joeylax54 - Mar-24-2020, 05:13 PM
RE: Hockey Data Project - by ndc85430 - Mar-24-2020, 07:17 PM
RE: Hockey Data Project - by Joeylax54 - Mar-24-2020, 08:07 PM
RE: Hockey Data Project - by ndc85430 - Mar-25-2020, 03:30 AM
RE: Hockey Data Project - by Joeylax54 - Mar-26-2020, 08:25 PM
RE: Hockey Data Project - by ndc85430 - Mar-28-2020, 05:16 PM
RE: Hockey Data Project - by snippsat - Mar-28-2020, 10:25 PM
RE: Hockey Data Project - by Joeylax54 - Mar-29-2020, 05:20 AM
RE: Hockey Data Project - by yummykudos - Apr-18-2020, 02:20 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020