Web Scraping NBA stats from basketball-reference.com












-1














I am trying to webscrape NBA data but I cannot figure out what the problem is with my code. I constantly get an error message saying:



Traceback (most recent call last):
File "/Users/mac/Desktop/intro2.py", line 17, in
df = df.append(pd.Series(row, index=columns), ignore_index=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/series.py", line 262, in init
.format(val=len(data), ind=len(index)))
ValueError: Length of passed values is 0, index implies 25



import requests
from bs4 import BeautifulSoup
import pandas as pd

URL = 'https://www.basketball-reference.com/friv/dailyleaders.fcgi?month=12&day=27&year=2018'
response = requests.get(URL)
soup = BeautifulSoup(response.text, 'html.parser')

columns = ['Rank', 'Player', 'Team', 'Game_location', 'Opponent', 'Win-loss', 'Minutes_played', 'Field_goals', 'Field_Goal_Attempts', 'Field_Goal_Percentage', '3Point_Field_Goals', '3-Point_Field_Goal_Attempts', 'Point_Field_Goal_Percentage', 'Free_Throws', 'Free_Throw_Attempts', 'Free_Throw_Percentage', 'Offensive_Rebounds', 'Defensive_Rebounds', 'Total_Rebounds', 'Assists', 'Blocks', 'Turnovers', 'Personal_Fouls', 'Points', 'Game_Score']
df = pd.DataFrame(columns=columns)

table = soup.find('table', attrs={'class': 'sortable', 'data-cols-to-freeze': '2'}).tbody
trs = table.find_all('tr')
for tr in trs:
tds = tr.find_all('td')
row = [td.text.replace('n', '') for td in tds]
df = df.append(pd.Series(row, index=columns), ignore_index=True)

df.to_csv('nba player of the week.csv', index=False)









share|improve this question







New contributor




DarkEquation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I'm sorry but we can't help you debug your program. We review working code in an effort to make it better. Once you get your program working we would love to take a look at it. Good Luck.
    – bruglesco
    6 mins ago










  • All right, thank you anyways!
    – DarkEquation
    3 mins ago
















-1














I am trying to webscrape NBA data but I cannot figure out what the problem is with my code. I constantly get an error message saying:



Traceback (most recent call last):
File "/Users/mac/Desktop/intro2.py", line 17, in
df = df.append(pd.Series(row, index=columns), ignore_index=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/series.py", line 262, in init
.format(val=len(data), ind=len(index)))
ValueError: Length of passed values is 0, index implies 25



import requests
from bs4 import BeautifulSoup
import pandas as pd

URL = 'https://www.basketball-reference.com/friv/dailyleaders.fcgi?month=12&day=27&year=2018'
response = requests.get(URL)
soup = BeautifulSoup(response.text, 'html.parser')

columns = ['Rank', 'Player', 'Team', 'Game_location', 'Opponent', 'Win-loss', 'Minutes_played', 'Field_goals', 'Field_Goal_Attempts', 'Field_Goal_Percentage', '3Point_Field_Goals', '3-Point_Field_Goal_Attempts', 'Point_Field_Goal_Percentage', 'Free_Throws', 'Free_Throw_Attempts', 'Free_Throw_Percentage', 'Offensive_Rebounds', 'Defensive_Rebounds', 'Total_Rebounds', 'Assists', 'Blocks', 'Turnovers', 'Personal_Fouls', 'Points', 'Game_Score']
df = pd.DataFrame(columns=columns)

table = soup.find('table', attrs={'class': 'sortable', 'data-cols-to-freeze': '2'}).tbody
trs = table.find_all('tr')
for tr in trs:
tds = tr.find_all('td')
row = [td.text.replace('n', '') for td in tds]
df = df.append(pd.Series(row, index=columns), ignore_index=True)

df.to_csv('nba player of the week.csv', index=False)









share|improve this question







New contributor




DarkEquation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I'm sorry but we can't help you debug your program. We review working code in an effort to make it better. Once you get your program working we would love to take a look at it. Good Luck.
    – bruglesco
    6 mins ago










  • All right, thank you anyways!
    – DarkEquation
    3 mins ago














-1












-1








-1







I am trying to webscrape NBA data but I cannot figure out what the problem is with my code. I constantly get an error message saying:



Traceback (most recent call last):
File "/Users/mac/Desktop/intro2.py", line 17, in
df = df.append(pd.Series(row, index=columns), ignore_index=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/series.py", line 262, in init
.format(val=len(data), ind=len(index)))
ValueError: Length of passed values is 0, index implies 25



import requests
from bs4 import BeautifulSoup
import pandas as pd

URL = 'https://www.basketball-reference.com/friv/dailyleaders.fcgi?month=12&day=27&year=2018'
response = requests.get(URL)
soup = BeautifulSoup(response.text, 'html.parser')

columns = ['Rank', 'Player', 'Team', 'Game_location', 'Opponent', 'Win-loss', 'Minutes_played', 'Field_goals', 'Field_Goal_Attempts', 'Field_Goal_Percentage', '3Point_Field_Goals', '3-Point_Field_Goal_Attempts', 'Point_Field_Goal_Percentage', 'Free_Throws', 'Free_Throw_Attempts', 'Free_Throw_Percentage', 'Offensive_Rebounds', 'Defensive_Rebounds', 'Total_Rebounds', 'Assists', 'Blocks', 'Turnovers', 'Personal_Fouls', 'Points', 'Game_Score']
df = pd.DataFrame(columns=columns)

table = soup.find('table', attrs={'class': 'sortable', 'data-cols-to-freeze': '2'}).tbody
trs = table.find_all('tr')
for tr in trs:
tds = tr.find_all('td')
row = [td.text.replace('n', '') for td in tds]
df = df.append(pd.Series(row, index=columns), ignore_index=True)

df.to_csv('nba player of the week.csv', index=False)









share|improve this question







New contributor




DarkEquation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am trying to webscrape NBA data but I cannot figure out what the problem is with my code. I constantly get an error message saying:



Traceback (most recent call last):
File "/Users/mac/Desktop/intro2.py", line 17, in
df = df.append(pd.Series(row, index=columns), ignore_index=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/series.py", line 262, in init
.format(val=len(data), ind=len(index)))
ValueError: Length of passed values is 0, index implies 25



import requests
from bs4 import BeautifulSoup
import pandas as pd

URL = 'https://www.basketball-reference.com/friv/dailyleaders.fcgi?month=12&day=27&year=2018'
response = requests.get(URL)
soup = BeautifulSoup(response.text, 'html.parser')

columns = ['Rank', 'Player', 'Team', 'Game_location', 'Opponent', 'Win-loss', 'Minutes_played', 'Field_goals', 'Field_Goal_Attempts', 'Field_Goal_Percentage', '3Point_Field_Goals', '3-Point_Field_Goal_Attempts', 'Point_Field_Goal_Percentage', 'Free_Throws', 'Free_Throw_Attempts', 'Free_Throw_Percentage', 'Offensive_Rebounds', 'Defensive_Rebounds', 'Total_Rebounds', 'Assists', 'Blocks', 'Turnovers', 'Personal_Fouls', 'Points', 'Game_Score']
df = pd.DataFrame(columns=columns)

table = soup.find('table', attrs={'class': 'sortable', 'data-cols-to-freeze': '2'}).tbody
trs = table.find_all('tr')
for tr in trs:
tds = tr.find_all('td')
row = [td.text.replace('n', '') for td in tds]
df = df.append(pd.Series(row, index=columns), ignore_index=True)

df.to_csv('nba player of the week.csv', index=False)






python web-scraping






share|improve this question







New contributor




DarkEquation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




DarkEquation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




DarkEquation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 13 mins ago









DarkEquation

11




11




New contributor




DarkEquation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





DarkEquation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






DarkEquation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • I'm sorry but we can't help you debug your program. We review working code in an effort to make it better. Once you get your program working we would love to take a look at it. Good Luck.
    – bruglesco
    6 mins ago










  • All right, thank you anyways!
    – DarkEquation
    3 mins ago


















  • I'm sorry but we can't help you debug your program. We review working code in an effort to make it better. Once you get your program working we would love to take a look at it. Good Luck.
    – bruglesco
    6 mins ago










  • All right, thank you anyways!
    – DarkEquation
    3 mins ago
















I'm sorry but we can't help you debug your program. We review working code in an effort to make it better. Once you get your program working we would love to take a look at it. Good Luck.
– bruglesco
6 mins ago




I'm sorry but we can't help you debug your program. We review working code in an effort to make it better. Once you get your program working we would love to take a look at it. Good Luck.
– bruglesco
6 mins ago












All right, thank you anyways!
– DarkEquation
3 mins ago




All right, thank you anyways!
– DarkEquation
3 mins ago















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

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: "196"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});


}
});






DarkEquation is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210553%2fweb-scraping-nba-stats-from-basketball-reference-com%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








DarkEquation is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















DarkEquation is a new contributor. Be nice, and check out our Code of Conduct.













DarkEquation is a new contributor. Be nice, and check out our Code of Conduct.












DarkEquation is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • 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.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210553%2fweb-scraping-nba-stats-from-basketball-reference-com%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre