Reputation: 689
I am having an issue scraping all 5 PlayerId's and duration from this liveData feed. By listing the [0]
element i am able to get 1 of the 5 players. I would like to get all 5 at the same time under the same Header HomePlayer
and AwayPlayer
also HomeDuration
and AwayDuration
. I tried copying HomePlayer
Mulitple times and changing the [0]
to [1]
but it will only scrape on of them not both which makes sense.
import requests
import csv
req = requests.get('https://statsapi.web.nhl.com/api/v1/game/2017020397/feed/live?site=en_nhl')
data = req.json()
my_data =[]
team_home, team_away = data['liveData']['boxscore']['teams']['home'], data['liveData']['boxscore']['teams']['away']
HomePlayer = team_home['onIcePlus'][0]['playerId']
AwayPlayer = team_away['onIcePlus'][0]['playerId']
HomeDuration = team_home['onIcePlus'][0]['shiftDuration']
AwayDuration = team_away['onIcePlus'][0]['shiftDuration']
my_data.append([HomePlayer, HomeDuration, AwayPlayer, AwayDuration])
headers = ["HomePlayer", "HomeDuration", "AwayPlayer", "AwayDuration"]
with open("NHL_2017020397_PbP_Shift.csv", "a", newline='') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(my_data)
f.close()
Upvotes: 1
Views: 80
Reputation: 6280
You are only accessing data for one player. So it is only writing that one player's data.
If you are sure that 5 records will be returned from the API then add a for
block inside the CSV
block to access each Player's data using the index on the array and writing them down one by one.
Check the code below. I moved the whole data logic into the CSV
block to loop through the list of players and write them down.
While this not a very Python way of doing it, you should do rest of the looking up to improve the code quality.
import requests
import csv
req = requests.get('https://statsapi.web.nhl.com/api/v1/game/2017020397/feed/live?site=en_nhl')
data = req.json()
team_home, team_away = data['liveData']['boxscore']['teams']['home'], data['liveData']['boxscore']['teams']['away']
headers = ["HomePlayer", "HomeDuration", "AwayPlayer", "AwayDuration"]
with open("/Users/kaybus/Desktop/NHL_2017020397_PbP_Shift.csv", "a", newline='') as f:
writer = csv.writer(f)
writer.writerow(headers)
for i in range(5):
HomePlayer = team_home['onIcePlus'][i]['playerId']
AwayPlayer = team_away['onIcePlus'][i]['playerId']
HomeDuration = team_home['onIcePlus'][i]['shiftDuration']
AwayDuration = team_away['onIcePlus'][i]['shiftDuration']
my_data =[]
my_data.append([HomePlayer, HomeDuration, AwayPlayer, AwayDuration])
writer.writerows(my_data)
Upvotes: 1