Hi,
I'm a bit of a novice with Python in terms of creating scripts (I tend to edit / read-only) and am really struggling to understand how to use loops for accessing the Fantasy Premier League API, if this is at all the way to do it. It may be FPL, but it also helps me upskill my Python skills for work!
From the FPL API endpoint https://fantasy.premierleague.com/api/event/36/live/, my objective is to extract to CSV all the "stats" attributes, with each one as an individual header column associated to the related ID. I can only extract the "stats" (delimited) and "ID" based on the parent-child structure, but need to have the following:
Anthony
I'm a bit of a novice with Python in terms of creating scripts (I tend to edit / read-only) and am really struggling to understand how to use loops for accessing the Fantasy Premier League API, if this is at all the way to do it. It may be FPL, but it also helps me upskill my Python skills for work!
From the FPL API endpoint https://fantasy.premierleague.com/api/event/36/live/, my objective is to extract to CSV all the "stats" attributes, with each one as an individual header column associated to the related ID. I can only extract the "stats" (delimited) and "ID" based on the parent-child structure, but need to have the following:
Output:ID Minutes Goals Scored Assists Clean Sheets etc.
5 90 0 0 0 1
Code so far:import requests import json import numpy as np import pandas as pd import datetime # Make a get request to get the latest player data from the FPL API link = "https://fantasy.premierleague.com/api/event/36/live/" response = requests.get(link) # Convert JSON data to a python object data = json.loads(response.text) # Initialize array to hold ALL player data # This will be a 2D array where each row is a different player all_players = [] # Loop through each player in the data for i in data["elements"]: id = i['id'] assists = i['assists'] goals_scored = i['goals_scored'] # Create a 1D array of the current players stats individual_stats = [id, assists, goals_scored] # Append the player array to a 2D array of all players all_players.append(individual_stats) # Convert the 2D array to a numpy array all_players = np.array(all_players) # Convert the numpy array to a pandas dataframe (table) dataset = pd.DataFrame({'id': all_players[:, 0], 'goals': all_players[:, 1], 'assists': all_players[:, 2]}) # Generate a unique filename based on date filename = str(datetime.datetime.today().date()) + '_fpl_players_weekly)' # Save the table of data as a CSV dataset.to_csv(index=False, path_or_buf=filename)JSON Response eg:
Output:"elements": [
{
"id": 1,
"stats": {
"minutes": 0,
"goals_scored": 0,
"assists": 0,
"clean_sheets": 0,
"goals_conceded": 0,
"own_goals": 0,
"penalties_saved": 0,
"penalties_missed": 0,
"yellow_cards": 0,
"red_cards": 0,
"saves": 0,
"bonus": 0,
"bps": 0,
"influence": "0.0",
"creativity": "0.0",
"threat": "0.0",
"ict_index": "0.0",
"starts": 0,
"expected_goals": "0.00",
"expected_assists": "0.00",
"expected_goal_involvements": "0.00",
"expected_goals_conceded": "0.00",
"total_points": 0,
"in_dreamteam": false
},
"explain": [
{
"fixture": 351,
"stats": [
{
"identifier": "minutes",
"points": 0,
"value": 0
}
]
}
]
},
{
"id": 2,
"stats": {
"minutes": 0,
"goals_scored": 0,
"assists": 0,
"clean_sheets": 0,
"goals_conceded": 0,
"own_goals": 0,
"penalties_saved": 0,
"penalties_missed": 0,
"yellow_cards": 0,
"red_cards": 0,
"saves": 0,
"bonus": 0,
"bps": 0,
"influence": "0.0",
"creativity": "0.0",
"threat": "0.0",
"ict_index": "0.0",
"starts": 0,
"expected_goals": "0.00",
"expected_assists": "0.00",
"expected_goal_involvements": "0.00",
"expected_goals_conceded": "0.00",
"total_points": 0,
"in_dreamteam": false
},
"explain": [
{
"fixture": 351,
"stats": [
{
"identifier": "minutes",
"points": 0,
"value": 0
}
]
}
]
}
ThanksAnthony
buran write May-10-2024, 04:04 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.