From b88db78b1e2885d51f3edd7084d5026b400daa35 Mon Sep 17 00:00:00 2001 From: Rasmus Luha Date: Wed, 16 Mar 2022 02:46:20 +0200 Subject: Players requests --- getData.py | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/getData.py b/getData.py index 466ed45..db11dc3 100644 --- a/getData.py +++ b/getData.py @@ -21,15 +21,15 @@ else: playersDir = "Data/Players" # Requesting data about NBA teams -url = "https://free-nba.p.rapidapi.com/teams" +url = "https://free-nba.p.rapidapi.com/" querystring = {"page":"0"} headers = { - 'x-rapidapi-host': "free-nba.p.rapidapi.com", - 'x-rapidapi-key': API_KEY + 'x-rapidapi-host': "free-nba.p.rapidapi.com", + 'x-rapidapi-key': API_KEY } # Adding data to teams file -response = requests.request("GET", url, headers=headers, params=querystring) +response = requests.request("GET", url+"teams", headers=headers, params=querystring) teamsDf = pd.DataFrame(response.json()["data"]) teamsDf.set_index("id") teamsDf = teamsDf.drop("id", axis=1) @@ -37,3 +37,40 @@ teamsDf = teamsDf.drop("id", axis=1) teamsDf.to_csv(teamsFile) ####################################################### +# Now requesting players for each team +# First request is made just to get the amount of pages that must be looped through + +querystring = {"per_page":"100","page":"0"} +response = requests.request("GET", url+"players", headers=headers, params=querystring) +pageCount = response.json()["meta"]["total_pages"] + + +for el in range(1, pageCount+1): + + # Requesting pages in loop till pageCount + querystring = {"per_page":"100","page":el} + response = requests.request("GET", url+"players", headers=headers, params=querystring) + data = response.json()["data"] + + for player in data: + teamName = player["team"]["full_name"] + playerDf = pd.DataFrame(columns=["first_name", "last_name", "position", "height_feet", "height_inches"]) + + playerSeries = pd.Series({"first_name" : player["first_name"], + "last_name" : player["last_name"], + "position" : player["position"], + "height_feet" : player["height_feet"], + "height_inches" : player["height_inches"]}) + + #add player to dataframe + playerDf.loc[len(playerDf)] = playerSeries + #add dataframe to File + playerDf.to_csv(playersDir+teamName, mode='a', index=False, header=False) + print("Page "+el+" read.") +print("All done, check Data Dir") + + + + + + -- cgit v1.2.3