Update api_scraper.py
Browse files- api_scraper.py +19 -19
api_scraper.py
CHANGED
|
@@ -874,30 +874,30 @@ class MLB_Scrape:
|
|
| 874 |
game_type_str = ','.join([str(x) for x in game_type])
|
| 875 |
|
| 876 |
if game_type_str == 'S':
|
| 877 |
-
player_data_1 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{1}/players?season={season}&gameType=[{game_type_str}]').json()
|
| 878 |
-
player_data_2 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{11}/players?season={season}&gameType=[{game_type_str}]').json()
|
| 879 |
-
player_data_3 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{12}/players?season={season}&gameType=[{game_type_str}]').json()
|
| 880 |
-
player_data_4 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{13}/players?season={season}&gameType=[{game_type_str}]').json()
|
| 881 |
-
player_data_5 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{14}/players?season={season}&gameType=[{game_type_str}]').json()
|
| 882 |
-
player_data =
|
| 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()
|
| 887 |
|
| 888 |
-
|
| 889 |
#Select relevant data that will help distinguish players from one another
|
| 890 |
|
| 891 |
-
fullName_list = [x['fullName'] if 'fullName' in x else None for x in player_data
|
| 892 |
-
firstName_list = [x['firstName'] if 'firstName' in x else None for x in player_data
|
| 893 |
-
lastName_list = [x['lastName'] if 'lastName' in x else None for x in player_data
|
| 894 |
-
id_list = [x['id'] if 'id' in x else None for x in player_data
|
| 895 |
-
position_list = [x['primaryPosition']['abbreviation'] if 'primaryPosition' in x and 'abbreviation' in x['primaryPosition'] else None for x in player_data
|
| 896 |
-
team_list = [x['currentTeam']['id'] if 'currentTeam' in x and 'id' in x['currentTeam'] else None for x in player_data
|
| 897 |
-
weight_list = [x['weight'] if 'weight' in x else None for x in player_data
|
| 898 |
-
height_list = [x['height'] if 'height' in x else None for x in player_data
|
| 899 |
-
age_list = [x['currentAge'] if 'currentAge' in x else None for x in player_data
|
| 900 |
-
birthDate_list = [x['birthDate'] if 'birthDate' in x else None for x in player_data
|
|
|
|
| 901 |
|
| 902 |
|
| 903 |
df = pl.DataFrame(data={'player_id':id_list,
|
|
|
|
| 874 |
game_type_str = ','.join([str(x) for x in game_type])
|
| 875 |
|
| 876 |
if game_type_str == 'S':
|
| 877 |
+
player_data_1 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{1}/players?season={season}&gameType=[{game_type_str}]').json()['people']
|
| 878 |
+
player_data_2 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{11}/players?season={season}&gameType=[{game_type_str}]').json()['people']
|
| 879 |
+
player_data_3 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{12}/players?season={season}&gameType=[{game_type_str}]').json()['people']
|
| 880 |
+
player_data_4 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{13}/players?season={season}&gameType=[{game_type_str}]').json()['people']
|
| 881 |
+
player_data_5 = requests.get(url=f'https://statsapi.mlb.com/api/v1/sports/{14}/players?season={season}&gameType=[{game_type_str}]').json() ['people']
|
| 882 |
+
player_data = player_data_1 + player_data_2 + player_data_3 + player_data_4 + player_data_5
|
| 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 |
#Select relevant data that will help distinguish players from one another
|
| 889 |
|
| 890 |
+
fullName_list = [x['fullName'] if 'fullName' in x else None for x in player_data]
|
| 891 |
+
firstName_list = [x['firstName'] if 'firstName' in x else None for x in player_data]
|
| 892 |
+
lastName_list = [x['lastName'] if 'lastName' in x else None for x in player_data]
|
| 893 |
+
id_list = [x['id'] if 'id' in x else None for x in player_data]
|
| 894 |
+
position_list = [x['primaryPosition']['abbreviation'] if 'primaryPosition' in x and 'abbreviation' in x['primaryPosition'] else None for x in player_data]
|
| 895 |
+
team_list = [x['currentTeam']['id'] if 'currentTeam' in x and 'id' in x['currentTeam'] else None for x in player_data]
|
| 896 |
+
weight_list = [x['weight'] if 'weight' in x else None for x in player_data]
|
| 897 |
+
height_list = [x['height'] if 'height' in x else None for x in player_data]
|
| 898 |
+
age_list = [x['currentAge'] if 'currentAge' in x else None for x in player_data]
|
| 899 |
+
birthDate_list = [x['birthDate'] if 'birthDate' in x else None for x in player_data]
|
| 900 |
+
|
| 901 |
|
| 902 |
|
| 903 |
df = pl.DataFrame(data={'player_id':id_list,
|