Why is it the scraping process generating duplicates?
When i print df_goalie_per.head() I can see that I have duplicates for some reason, don't understand why, any suggestions?
I also checked the other DataFrames, and found that ONLY the df_goalie_
dataframes are affected by this. Does this have to do with the looping?
Tried changing the looping order but didn't get any success.
Code is below:
#Importing Libraries
import numpy as np
import pandas as pd
import requests
import json
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
#Create Empty lists
player_id = {}
goalie_id = {}
person =
position =
skaterstats =
goalie_person=
goalie_position=
goalie_stats=
team =
team_goals =
matchid =
#Connect to NHL-API
for game_id in range(2017020001, 2017020002, 1):
url = 'https://statsapi.web.nhl.com/api/v1/game/{}/feed/live'.format(game_id)
r = requests.get(url)
game_data = r.json()
#PLAYER SCRAPING
for homeaway in ['home','away']:
player_dict = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('skaters')
player_id[homeaway] = player_dict
#Get PlayerStats/TeamStats
for homeaway in player_id:
for playerID in player_id[homeaway]:
play_dict_teamname = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('team').get('name')
play_dict_teamgoals = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('teamStats').get('teamSkaterStats').get('goals')
play_dict_gameid = game_data.get('gamePk')
play_dict_person = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('person')
play_dict_position = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('position')
play_dict_skaterstats = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('stats').get('skaterStats')
#Append TeamStats to Empty list
team.append(play_dict_teamname)
team_goals.append(play_dict_teamgoals)
matchid.append(play_dict_gameid)
#Append PlayerStats to Empty list
person.append(play_dict_person)
position.append(play_dict_position)
if play_dict_skaterstats:
skaterstats.append(play_dict_skaterstats)
#GOALIE SCRAPING
for homeaway in ['home','away']:
goalie_dict = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('goalies')
goalie_id[homeaway] = goalie_dict
#Get GoalieStats
for homeaway in goalie_id:
for goalieID in goalie_id[homeaway]:
goal_dict_person = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('person')
goal_dict_position = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('position')
goal_dict_stats = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('stats').get('goalieStats')
#Append GoalieStats to Empty list
goalie_person.append(goal_dict_person)
goalie_position.append(goal_dict_position)
if goal_dict_stats:
goalie_stats.append(goal_dict_stats)
#Create DataFrames for all lists
df_person = pd.DataFrame(person)
df_position = pd.DataFrame(position)
df_skaterstats = pd.DataFrame(skaterstats)
df_team = pd.DataFrame(team)
df_teamgoals = pd.DataFrame(team_goals)
df_gameID = pd.DataFrame(matchid)
df_goalie_per = pd.DataFrame(goalie_person)
df_goalie_pos = pd.DataFrame(goalie_position)
df_goalie_stats = pd.DataFrame(goalie_stats)
df_goalie_per.head()
fullName id link rosterStatus shootsCatches
0 Steve Mason 8473461 /api/v1/people/8473461 Y R
1 Connor Hellebuyck 8476945 /api/v1/people/8476945 Y L
2 Steve Mason 8473461 /api/v1/people/8473461 Y R
3 Connor Hellebuyck 8476945 /api/v1/people/8476945 Y L
4 Frederik Andersen 8475883 /api/v1/people/8475883 Y L
python python-3.x api for-loop iteration
add a comment |
When i print df_goalie_per.head() I can see that I have duplicates for some reason, don't understand why, any suggestions?
I also checked the other DataFrames, and found that ONLY the df_goalie_
dataframes are affected by this. Does this have to do with the looping?
Tried changing the looping order but didn't get any success.
Code is below:
#Importing Libraries
import numpy as np
import pandas as pd
import requests
import json
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
#Create Empty lists
player_id = {}
goalie_id = {}
person =
position =
skaterstats =
goalie_person=
goalie_position=
goalie_stats=
team =
team_goals =
matchid =
#Connect to NHL-API
for game_id in range(2017020001, 2017020002, 1):
url = 'https://statsapi.web.nhl.com/api/v1/game/{}/feed/live'.format(game_id)
r = requests.get(url)
game_data = r.json()
#PLAYER SCRAPING
for homeaway in ['home','away']:
player_dict = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('skaters')
player_id[homeaway] = player_dict
#Get PlayerStats/TeamStats
for homeaway in player_id:
for playerID in player_id[homeaway]:
play_dict_teamname = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('team').get('name')
play_dict_teamgoals = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('teamStats').get('teamSkaterStats').get('goals')
play_dict_gameid = game_data.get('gamePk')
play_dict_person = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('person')
play_dict_position = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('position')
play_dict_skaterstats = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('stats').get('skaterStats')
#Append TeamStats to Empty list
team.append(play_dict_teamname)
team_goals.append(play_dict_teamgoals)
matchid.append(play_dict_gameid)
#Append PlayerStats to Empty list
person.append(play_dict_person)
position.append(play_dict_position)
if play_dict_skaterstats:
skaterstats.append(play_dict_skaterstats)
#GOALIE SCRAPING
for homeaway in ['home','away']:
goalie_dict = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('goalies')
goalie_id[homeaway] = goalie_dict
#Get GoalieStats
for homeaway in goalie_id:
for goalieID in goalie_id[homeaway]:
goal_dict_person = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('person')
goal_dict_position = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('position')
goal_dict_stats = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('stats').get('goalieStats')
#Append GoalieStats to Empty list
goalie_person.append(goal_dict_person)
goalie_position.append(goal_dict_position)
if goal_dict_stats:
goalie_stats.append(goal_dict_stats)
#Create DataFrames for all lists
df_person = pd.DataFrame(person)
df_position = pd.DataFrame(position)
df_skaterstats = pd.DataFrame(skaterstats)
df_team = pd.DataFrame(team)
df_teamgoals = pd.DataFrame(team_goals)
df_gameID = pd.DataFrame(matchid)
df_goalie_per = pd.DataFrame(goalie_person)
df_goalie_pos = pd.DataFrame(goalie_position)
df_goalie_stats = pd.DataFrame(goalie_stats)
df_goalie_per.head()
fullName id link rosterStatus shootsCatches
0 Steve Mason 8473461 /api/v1/people/8473461 Y R
1 Connor Hellebuyck 8476945 /api/v1/people/8476945 Y L
2 Steve Mason 8473461 /api/v1/people/8473461 Y R
3 Connor Hellebuyck 8476945 /api/v1/people/8476945 Y L
4 Frederik Andersen 8475883 /api/v1/people/8475883 Y L
python python-3.x api for-loop iteration
add a comment |
When i print df_goalie_per.head() I can see that I have duplicates for some reason, don't understand why, any suggestions?
I also checked the other DataFrames, and found that ONLY the df_goalie_
dataframes are affected by this. Does this have to do with the looping?
Tried changing the looping order but didn't get any success.
Code is below:
#Importing Libraries
import numpy as np
import pandas as pd
import requests
import json
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
#Create Empty lists
player_id = {}
goalie_id = {}
person =
position =
skaterstats =
goalie_person=
goalie_position=
goalie_stats=
team =
team_goals =
matchid =
#Connect to NHL-API
for game_id in range(2017020001, 2017020002, 1):
url = 'https://statsapi.web.nhl.com/api/v1/game/{}/feed/live'.format(game_id)
r = requests.get(url)
game_data = r.json()
#PLAYER SCRAPING
for homeaway in ['home','away']:
player_dict = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('skaters')
player_id[homeaway] = player_dict
#Get PlayerStats/TeamStats
for homeaway in player_id:
for playerID in player_id[homeaway]:
play_dict_teamname = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('team').get('name')
play_dict_teamgoals = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('teamStats').get('teamSkaterStats').get('goals')
play_dict_gameid = game_data.get('gamePk')
play_dict_person = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('person')
play_dict_position = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('position')
play_dict_skaterstats = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('stats').get('skaterStats')
#Append TeamStats to Empty list
team.append(play_dict_teamname)
team_goals.append(play_dict_teamgoals)
matchid.append(play_dict_gameid)
#Append PlayerStats to Empty list
person.append(play_dict_person)
position.append(play_dict_position)
if play_dict_skaterstats:
skaterstats.append(play_dict_skaterstats)
#GOALIE SCRAPING
for homeaway in ['home','away']:
goalie_dict = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('goalies')
goalie_id[homeaway] = goalie_dict
#Get GoalieStats
for homeaway in goalie_id:
for goalieID in goalie_id[homeaway]:
goal_dict_person = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('person')
goal_dict_position = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('position')
goal_dict_stats = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('stats').get('goalieStats')
#Append GoalieStats to Empty list
goalie_person.append(goal_dict_person)
goalie_position.append(goal_dict_position)
if goal_dict_stats:
goalie_stats.append(goal_dict_stats)
#Create DataFrames for all lists
df_person = pd.DataFrame(person)
df_position = pd.DataFrame(position)
df_skaterstats = pd.DataFrame(skaterstats)
df_team = pd.DataFrame(team)
df_teamgoals = pd.DataFrame(team_goals)
df_gameID = pd.DataFrame(matchid)
df_goalie_per = pd.DataFrame(goalie_person)
df_goalie_pos = pd.DataFrame(goalie_position)
df_goalie_stats = pd.DataFrame(goalie_stats)
df_goalie_per.head()
fullName id link rosterStatus shootsCatches
0 Steve Mason 8473461 /api/v1/people/8473461 Y R
1 Connor Hellebuyck 8476945 /api/v1/people/8476945 Y L
2 Steve Mason 8473461 /api/v1/people/8473461 Y R
3 Connor Hellebuyck 8476945 /api/v1/people/8476945 Y L
4 Frederik Andersen 8475883 /api/v1/people/8475883 Y L
python python-3.x api for-loop iteration
When i print df_goalie_per.head() I can see that I have duplicates for some reason, don't understand why, any suggestions?
I also checked the other DataFrames, and found that ONLY the df_goalie_
dataframes are affected by this. Does this have to do with the looping?
Tried changing the looping order but didn't get any success.
Code is below:
#Importing Libraries
import numpy as np
import pandas as pd
import requests
import json
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
#Create Empty lists
player_id = {}
goalie_id = {}
person =
position =
skaterstats =
goalie_person=
goalie_position=
goalie_stats=
team =
team_goals =
matchid =
#Connect to NHL-API
for game_id in range(2017020001, 2017020002, 1):
url = 'https://statsapi.web.nhl.com/api/v1/game/{}/feed/live'.format(game_id)
r = requests.get(url)
game_data = r.json()
#PLAYER SCRAPING
for homeaway in ['home','away']:
player_dict = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('skaters')
player_id[homeaway] = player_dict
#Get PlayerStats/TeamStats
for homeaway in player_id:
for playerID in player_id[homeaway]:
play_dict_teamname = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('team').get('name')
play_dict_teamgoals = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('teamStats').get('teamSkaterStats').get('goals')
play_dict_gameid = game_data.get('gamePk')
play_dict_person = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('person')
play_dict_position = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('position')
play_dict_skaterstats = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(playerID)).get('stats').get('skaterStats')
#Append TeamStats to Empty list
team.append(play_dict_teamname)
team_goals.append(play_dict_teamgoals)
matchid.append(play_dict_gameid)
#Append PlayerStats to Empty list
person.append(play_dict_person)
position.append(play_dict_position)
if play_dict_skaterstats:
skaterstats.append(play_dict_skaterstats)
#GOALIE SCRAPING
for homeaway in ['home','away']:
goalie_dict = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('goalies')
goalie_id[homeaway] = goalie_dict
#Get GoalieStats
for homeaway in goalie_id:
for goalieID in goalie_id[homeaway]:
goal_dict_person = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('person')
goal_dict_position = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('position')
goal_dict_stats = game_data.get('liveData').get('boxscore').get('teams').get(homeaway).get('players').get('ID' + str(goalieID)).get('stats').get('goalieStats')
#Append GoalieStats to Empty list
goalie_person.append(goal_dict_person)
goalie_position.append(goal_dict_position)
if goal_dict_stats:
goalie_stats.append(goal_dict_stats)
#Create DataFrames for all lists
df_person = pd.DataFrame(person)
df_position = pd.DataFrame(position)
df_skaterstats = pd.DataFrame(skaterstats)
df_team = pd.DataFrame(team)
df_teamgoals = pd.DataFrame(team_goals)
df_gameID = pd.DataFrame(matchid)
df_goalie_per = pd.DataFrame(goalie_person)
df_goalie_pos = pd.DataFrame(goalie_position)
df_goalie_stats = pd.DataFrame(goalie_stats)
df_goalie_per.head()
fullName id link rosterStatus shootsCatches
0 Steve Mason 8473461 /api/v1/people/8473461 Y R
1 Connor Hellebuyck 8476945 /api/v1/people/8476945 Y L
2 Steve Mason 8473461 /api/v1/people/8473461 Y R
3 Connor Hellebuyck 8476945 /api/v1/people/8476945 Y L
4 Frederik Andersen 8475883 /api/v1/people/8475883 Y L
python python-3.x api for-loop iteration
python python-3.x api for-loop iteration
asked Jan 2 at 18:21
MisterButterMisterButter
739
739
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011285%2fwhy-is-it-the-scraping-process-generating-duplicates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011285%2fwhy-is-it-the-scraping-process-generating-duplicates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown