└── scrape_NBA_data.py /scrape_NBA_data.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Dec 22 11:37:10 2019 4 | 5 | @author: Ken 6 | """ 7 | 8 | 9 | #Things you need: Player & Team IDs 10 | 11 | #eswan18 12 | 13 | # find player Ids 14 | from nba_api.stats.static import players 15 | player_dict = players.get_players() 16 | 17 | # Use ternary operator or write function 18 | bron = [player for player in player_dict if player['full_name'] == 'LeBron James'][0] 19 | bron_id = bron['id'] 20 | 21 | # find team Ids 22 | from nba_api.stats.static import teams 23 | teams = teams.get_teams() 24 | GSW = [x for x in teams if x['full_name'] == 'Golden State Warriors'][0] 25 | GSW_id = GSW['id'] 26 | 27 | #game_stats_player 28 | #could not get DateFrom to work, just use loop with years if necessary 29 | from nba_api.stats.library.parameters import SeasonAll 30 | from nba_api.stats.endpoints import playergamelog 31 | import pandas as pd 32 | 33 | gamelog_bron = playergamelog.PlayerGameLog(player_id='2544', season = '2018') 34 | df_bron_games_2018 = gamelog_bron.get_data_frames() 35 | 36 | gamelog_bron_all = playergamelog.PlayerGameLog(player_id='2544', season = SeasonAll.all) 37 | df_bron_games_all = gamelog_bron_all.get_data_frames() 38 | 39 | #find games played by a team or player 40 | from nba_api.stats.endpoints import leaguegamefinder 41 | GSW_games = leaguegamefinder.LeagueGameFinder(team_id_nullable=GSW_id).get_data_frames()[0] 42 | 43 | # game play by play data 44 | from nba_api.stats.endpoints import playbyplay 45 | pbp = playbyplay.PlayByPlay('0021900429').get_data_frames()[0] 46 | 47 | 48 | 49 | 50 | 51 | --------------------------------------------------------------------------------