Update api_scraper.py
Browse files- api_scraper.py +39 -32
api_scraper.py
CHANGED
|
@@ -874,42 +874,49 @@ class MLB_Scrape:
|
|
| 874 |
game_type_str = ','.join([str(x) for x in game_type])
|
| 875 |
|
| 876 |
if game_type_str == 'S':
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 884 |
|
| 885 |
else:
|
| 886 |
player_data = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{sport_id}/players?season={season}&gameType=[{game_type_str}]').json()['people']
|
| 887 |
|
| 888 |
-
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
| 908 |
-
|
| 909 |
-
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
|
| 913 |
|
| 914 |
return df
|
| 915 |
|
|
|
|
| 874 |
game_type_str = ','.join([str(x) for x in game_type])
|
| 875 |
|
| 876 |
if game_type_str == 'S':
|
| 877 |
+
player_data = requests.get(f'https://bdfed.stitch.mlbinfra.com/bdfed/stats/player?&env=prod&season={season}&sportId=1&stats=season&group=pitching&gameType=S&limit=1000000&offset=0&sortStat=inningsPitched&order=asc').json()
|
| 878 |
+
fullName_list = [x['playerFullName'] if 'playerFullName' in x else None for x in player_data['stats']]
|
| 879 |
+
firstName_list = [x['playerFirstName'] if 'playerFirstName' in x else None for x in player_data['stats']]
|
| 880 |
+
lastName_list = [x['playerLastName'] if 'playerLastName' in x else None for x in player_data['stats']]
|
| 881 |
+
id_list = [x['playerId'] if 'playerId' in x else None for x in player_data['stats']]
|
| 882 |
+
position_list = [x['primaryPositionAbbrev'] if 'primaryPositionAbbrev' in x else None for x in player_data['stats']]
|
| 883 |
+
team_list = [x['teamId'] if 'teamId' in x else None for x in player_data['stats']]
|
| 884 |
+
|
| 885 |
+
df = pl.DataFrame(data={'player_id':id_list,
|
| 886 |
+
'first_name':firstName_list,
|
| 887 |
+
'last_name':lastName_list,
|
| 888 |
+
'name':fullName_list,
|
| 889 |
+
'position':position_list,
|
| 890 |
+
'team':team_list})
|
| 891 |
|
| 892 |
else:
|
| 893 |
player_data = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{sport_id}/players?season={season}&gameType=[{game_type_str}]').json()['people']
|
| 894 |
|
| 895 |
+
#Select relevant data that will help distinguish players from one another
|
| 896 |
+
|
| 897 |
+
fullName_list = [x['fullName'] if 'fullName' in x else None for x in player_data]
|
| 898 |
+
firstName_list = [x['firstName'] if 'firstName' in x else None for x in player_data]
|
| 899 |
+
lastName_list = [x['lastName'] if 'lastName' in x else None for x in player_data]
|
| 900 |
+
id_list = [x['id'] if 'id' in x else None for x in player_data]
|
| 901 |
+
position_list = [x['primaryPosition']['abbreviation'] if 'primaryPosition' in x and 'abbreviation' in x['primaryPosition'] else None for x in player_data]
|
| 902 |
+
team_list = [x['currentTeam']['id'] if 'currentTeam' in x and 'id' in x['currentTeam'] else None for x in player_data]
|
| 903 |
+
weight_list = [x['weight'] if 'weight' in x else None for x in player_data]
|
| 904 |
+
height_list = [x['height'] if 'height' in x else None for x in player_data]
|
| 905 |
+
age_list = [x['currentAge'] if 'currentAge' in x else None for x in player_data]
|
| 906 |
+
birthDate_list = [x['birthDate'] if 'birthDate' in x else None for x in player_data]
|
| 907 |
+
|
| 908 |
+
|
| 909 |
+
|
| 910 |
+
df = pl.DataFrame(data={'player_id':id_list,
|
| 911 |
+
'first_name':firstName_list,
|
| 912 |
+
'last_name':lastName_list,
|
| 913 |
+
'name':fullName_list,
|
| 914 |
+
'position':position_list,
|
| 915 |
+
'team':team_list,
|
| 916 |
+
'weight':weight_list,
|
| 917 |
+
'height':height_list,
|
| 918 |
+
'age':age_list,
|
| 919 |
+
'birthDate':birthDate_list})
|
| 920 |
|
| 921 |
return df
|
| 922 |
|