def test_get_game_logs(self): expected_columns = [ 'DATE', 'AGE', 'TEAM', 'HOME/AWAY', 'OPPONENT', 'RESULT', 'GS', 'MP', 'FG', 'FGA', 'FG%', '3P', '3PA', '3P%', 'FT', 'FTA', 'FT%', 'ORB', 'DRB', 'TRB', 'AST', 'STL', 'BLK', 'TOV', 'PF', 'PTS', 'GAME_SCORE', '+/-' ] df = get_game_logs('Stephen Curry', '2015-10-11', '2016-10-11') self.assertCountEqual(list(df.columns), expected_columns) df = get_game_logs('Pau Gasol', '2010-01-12', '2010-01-20', playoffs=False) self.assertEqual(len(df), 2)
def view_mvp_candidates(): mvp_candidates = [ 'LeBron James', 'Giannis Antetokounmpo', 'James Harden', 'Anthony Davis', 'Kawhi Leonard', 'Jimmy Butler', 'Chris Paul' ] display_rank = 1 for mvp_candidate in mvp_candidates: print(f"Our Ranking #{display_rank}: {mvp_candidate}") display_rank += 1 print() mvp_candidate = input("Whose stats would like to see? ") # ADD Check Condition Here. # Loop if Player not one of the above. # Error when entry is 1. game_logs = get_game_logs(mvp_candidate, '2019-10-22', '2020-03-01', playoffs=False) game_stats = get_stats(mvp_candidate, stat_type='PER_GAME', playoffs=False, career=False) print(f"-----------{mvp_candidate}'s 2019-2020 GAME LOGS-----------") print(game_logs) print(f"-----------{mvp_candidate}'s CAREER GAME STATS-----------") print(game_stats)
def player_game_logs(player, start_date, end_date, playoffs): print("Saving into excel sheet... ") writer = ExcelWriter("./Excel-Sheets/%s_GAME_LOG.xlsx" % player) player_game_log_df = get_game_logs(player, start_date, end_date, playoffs) player_game_log_df.to_excel(writer, sheet_name=player, startrow=0, startcol=0, index=False, header=True) writer.save()
def get_players_game_log(players): """ scrapes the game log for the list of players and creates a CSV for each :param players: players to scrape data :return: void """ for player in players: # TODO: create cache to download only missing parts of the data. # TODO: can make easy cache to get last dates or cpmlex for all missing data try: temp = get_game_logs(player, "2020-10-10", "2021-10-10", playoffs=False) path = r"C:\Users\gilad\PycharmProjects\nba\log-" + str( player) + "20-21.csv" temp.to_csv(path, index=False) except: print("no data for " + player)
def user_input(): team_or_player = input("What stats would you like to see: (1)Team or (2)Player ") if team_or_player == '1': user_team = input("What team would you like to view? ") user_season = int(input("Enter the desired season: ")) user_stats = input("Enter the number for what stat you would like to see: \n1.) Roster List\n" "2.) Team Stats\n" "3.) Team Opponent Stats\n" "4.) Roster Stats\n" "5.) Misc Stats\n") print_or_excel = input("Would you like to save information in an excel sheet (Y/N)? ").upper() if user_stats == '1': if print_or_excel == 'Y': roster_list(user_team, user_season) print("Saved") elif print_or_excel == 'N': print(get_roster(user_team, user_season)) elif user_stats == '2': data_format = input("Enter data format: (TOTAL | PER_GAME): ").upper() if print_or_excel == 'Y': team_stats(user_team, user_season, data_format) print(get_team_stats(user_team, user_season, data_format)) elif print_or_excel == 'N': print(get_team_stats(user_team, user_season, data_format)) elif user_stats == '3': data_format = input("Enter data format: (TOTAL | PER_GAME: ").upper() if print_or_excel == 'Y': team_opp_stats(user_team, user_season, data_format) print(get_opp_stats(user_team, user_season, data_format)) elif print_or_excel == 'N' or 'n': print(get_opp_stats(user_team, user_season, data_format)) elif user_stats == '4': user_playoffs = input("Would you like playoff roster stats? (Y/N) ") data_format = input("Enter data format: (TOTAL | PER_GAME): ").upper() playoffs = False if user_playoffs == 'Y': playoffs = True if print_or_excel == 'Y': roster_stats(user_team, user_season, data_format, playoffs) print(get_roster_stats(user_team, user_season, data_format, playoffs)) elif print_or_excel == 'N': print(get_roster_stats(user_team, user_season, data_format, playoffs)) elif user_stats == '5': if print_or_excel == 'Y': team_misc(user_team, user_season) print(get_team_misc(user_team, user_season)) elif print_or_excel == 'N': print(get_team_misc(user_team, user_season)) elif team_or_player == '2': user_player = input('What player do you like to view? ') print_or_excel = input("Save into excel sheet? (Y/N): ").upper() user_stats = input("Enter the number for what stat you would like to see: \n" "1.) Player Stats\n" "2.) Game Logs\n") user_playoffs = input("Playoff Stats? (Y/N): ").upper() if user_stats == '1': stat_type = input("Enter stat type: (PER_GAME | PER_MINUTE | PER_POSS | ADVANCED): ").upper() user_career = input("Career Stats? (Y/N): ").upper() if user_playoffs == 'Y': playoffs = True else: playoffs = False if user_career == 'Y': career = True else: career = False if print_or_excel == "Y": player_stats(user_player, stat_type, playoffs, career) print(get_stats(user_player, stat_type, playoffs, career)) elif print_or_excel == "N": print(get_stats(user_player, stat_type, playoffs, career)) elif user_stats == '2': user_start_date = input("Enter start date: (YYYY-MM-DD): ").upper() user_end_date = input("Enter end date: (YYYY-MM-DD): ") if user_playoffs == 'Y': playoffs = True else: playoffs = False if print_or_excel == "Y": player_game_logs(user_player, user_start_date, user_end_date, playoffs) print(get_game_logs(user_player, user_start_date, user_end_date, playoffs)) elif print_or_excel == "N": print(get_game_logs(user_player, user_start_date, user_end_date, playoffs))
html = urlopen(url) except: match = True soup = BeautifulSoup(html, features="html.parser") player_page = soup.find('h1', itemprop="name").getText().strip() if player_page == player_name: return url id_num += 1 return def get_player_url_endpoint(player_name, num): first_name, last_name = player_name.split(' ') url_endpoint = last_name[0] + '/' digit_id = '0' + str(num) if len(last_name) < 5: url_endpoint += last_name + first_name[:2] + digit_id + '.html' else: url_endpoint += last_name[:5] + first_name[:2] + digit_id + '.html' # print("url_endpoint:", url_endpoint.lower()) return url_endpoint.lower() # scrape_player_season_data('Ray Allen') stats = get_game_logs('Ray Allen', '2010-01-01', '2014-04-01', False) print(stats)
# players_file.close() print('\n\nGetting Game Logs\n\n') #Get_game_logs for each player and Save into .csv file players = iter(allPlayers) for player in players: #Maybe dont need if player is None: next(players) print(f'\n\n{player} logs \n') spacesRemoved = player.replace(" ", "") if spacesRemoved.isalpha() == False: next(players) else: gameLogs = get_game_logs(player, oneYear, dateString) #Save gamelogs to .csv if gameLogs is None: next(players) else: gameLogs.to_csv(f'PlayerGameLogs/{player}.csv', index=False) print(gameLogs) ########################################################################### ###################WORK IN PROGRESS ######################################## #######Trying to figure out how to save a dataframe to SQL # https://www.dataquest.io/blog/sql-insert-tutorial/ # #One Player in Game logs # gameLog = get_game_logs('Stephen Curry', oneYear, dateString)
s = get_roster_stats('GSW', 2019, data_format='PER_GAME', playoffs=False) print(s) s = get_team_misc('GSW', 2019) print(s) from basketball_reference_scraper.players import get_stats, get_game_logs s = get_stats('Stephen Curry', stat_type='PER_GAME', playoffs=False, career=False) print(s) df = get_game_logs('LeBron James', '2010-01-19', '2014-01-20', playoffs=False) print(df) from basketball_reference_scraper.seasons import get_schedule, get_standings s = get_schedule(2018, playoffs=False) print(s) s = get_standings(date='2020-01-06') print(s) from basketball_reference_scraper.box_scores import get_box_scores s = get_box_scores('2020-01-13', 'CHI', 'BOS',
# Loop for pulling roster data ''' output = [] for team in teams: df1 = get_roster(team, 2021) df1 = df1[['PLAYER','POS']] df1['TEAM'] = team output.append(df1) results = pd.concat(output,ignore_index=True) results.to_csv('2021 Roster.csv') ''' df = pd.read_csv('2020 Playoff Roster.csv', index_col = 0) df.drop_duplicates(subset=['PLAYER'],keep='first',inplace=True) df = list(df['PLAYER']) #df = df[165:] #print(get_game_logs('Joel Embiid','2020-08-01', '2020-10-11', playoffs=True)) for i,player in enumerate(df): if i == 0: game_log = get_game_logs(player, '2020-08-01', '2020-10-11', playoffs=True) df_to_add = get_game_logs(player, '2020-08-01', '2020-10-11', playoffs=True) game_log = pd.concat([game_log, df_to_add]) game_log.to_csv('2019-2020 Game Log Playoffs.csv')
from basketball_reference_scraper.teams import get_roster, get_team_stats, get_opp_stats, get_roster_stats, get_team_misc from basketball_reference_scraper.players import get_stats, get_game_logs import pandas as pd #Read Roster CSV df = pd.read_csv('2019 Playoff Roster.csv', index_col=0) # for Keep, we want last, because a player could have been traded to another team. They would only play for Playoffs for that last team df.drop_duplicates(subset=['PLAYER'], keep='last', inplace=True) #pd.set_option('display.max_rows', df.shape[0]+1) #print(df.duplicated()) df = list(df['PLAYER']) #df = df[50:] #print(get_game_logs('Jayson Tatum','2019-04-13', '2019-06-13', playoffs=True)) for i, player in enumerate(df): if i == 0: game_log = get_game_logs(player, '2018-10-16', '2019-04-10', playoffs=False) df_to_add = get_game_logs(player, '2018-10-16', '2019-04-10', playoffs=False) game_log = pd.concat([game_log, df_to_add]) game_log.to_csv('2018-2019 Game Log.csv')