Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSV output problem
#1
Hi guys-New Python user here. I enjoy working with basketball statistics and have realized I need to figure out a more efficient way to scrape data from NBA.com. I found this script on the internet and have managed to (I think) get pretty close it working, but when I open Powershell and type "python NBA2.py" (file name) I get no response. Powershell stays open but doesn't give any feedback or let me type anymore. FYI I'm using Windows 10 and Python 3.6

import requests
import csv

url = "http://stats.nba.com/stats/leaguedashplayerstats?DateFrom=&DateTo=&GameScope=&GameSegment=&LastNGames=15&LeagueID=00&Location=&MeasureType=Advanced&Month=0&OpponentTeamID=0&Outcome=&PaceAdjust=N&PerMode=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2015-16&SeasonSegment=&SeasonType=Regular+Season&StarterBench=&VsConference=&VsDivision="

data = requests.get(url)
entries = data.json()

with open('output.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(entries['resultSets'][0]['headers'])
    csv_output.writerows(entries['resultSets'][0]['rowSet'])
If anyone could provide some info on why this isn't working or point me toward a website that can help it would be greatly appreciated. I've spent hours troubleshooting and can't seem to pinpoint where the problem is.
Reply
#2
take a look at:
https://python-forum.io/Thread-Web-Scraping-part-1
and
https://python-forum.io/Thread-Web-scraping-part-2
Reply
#3
(Oct-29-2017, 12:57 AM)Larz60+ Wrote: take a look at:
https://python-forum.io/Thread-Web-Scraping-part-1
and
https://python-forum.io/Thread-Web-scraping-part-2

Thanks. Those links were helpful but don't really solve my issue. I guess "scrape" isn't really the proper terminology for what I need to do (like I said, new at this Doh). The url in my script takes me directly to a web page with the data separated by commas. From there I just need get it into a format that I can analyze within Excel. I feel like the script I posted should do that, but I must be missing something.
Reply
#4
If you click on the url that you have, you get the following response:
Quote:The field SeasonType must match the regular expression '^(Regular Season)|(Pre Season)|(Playoffs)|(All Star)$'.; The StarterBench property is required.; The VsConference property is required.; The VsDivision property is required.
Reply
#5
(Oct-29-2017, 03:12 AM)Larz60+ Wrote: If you click on the url that you have, you get the following response:
Quote:The field SeasonType must match the regular expression '^(Regular Season)|(Pre Season)|(Playoffs)|(All Star)$'.; The StarterBench property is required.; The VsConference property is required.; The VsDivision property is required.

It works when you copy/paste. For some reason the hyperlink stops before the end of the URL.
Reply
#6
Even so, requests for whatever reason freezes up and doesn't even timeout
I believe that the syntax that you have (for the json load) is correct, but I would still look at the errors that
are in the message above.

There is a different way to pass params, see: https://stackoverflow.com/questions/6386...-in-python

and another here: https://stackoverflow.com/questions/1751...ter-python
Reply
#7
(Oct-29-2017, 05:08 AM)Larz60+ Wrote: Even so, requests for whatever reason freezes up and doesn't even timeout
I believe that the syntax that you have (for the json load) is correct, but I would still look at the errors that
are in the message above.

There is a different way to pass params, see: https://stackoverflow.com/questions/6386...-in-python

and another here: https://stackoverflow.com/questions/1751...ter-python

Shot in the dark here but could there be a problem with python reading the plus sign (+) within the link?
Reply
#8
After altering the link I managed to get this response when running in PowerShell. Trying to figure out on my own how to fix this but haven't come up with anything.

PS C:\Users\Nick> python NBASample.py
Traceback (most recent call last):
File "NBASample.py", line 11, in <module>
csv_output.writerow(entries['resultSet'][0]['headers'])
KeyError: 0

import requests
import csv

url = "http://stats.nba.com/stats/leagueLeaders?LeagueID=00&PerMode=PerGame&Scope=S&Season=2017-18&SeasonType=Regular+Season&StatCategory=PTS"

data = requests.get(url)
entries = data.json()

with open('NBAFile.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(entries['resultSet'][0]['headers'])
    csv_output.writerows(entries['resultSet'][0]['rowSet'])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 413 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  output shape problem with np.arange alan6690 5 738 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  problem in output of a function akbarza 9 1,239 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 944 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Facing problem with Pycharm - Not getting the expected output amortal03 1 871 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  single input infinite output problem Chase91 2 1,969 Sep-23-2020, 10:01 PM
Last Post: Chase91
  Output to a json file problem Netcode 3 3,759 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  DHT11 output to website problem cjdock 0 1,483 Oct-01-2019, 08:29 PM
Last Post: cjdock
  Problem Table Output Phil 4 2,680 May-19-2019, 12:17 PM
Last Post: Phil
  Pass variable script return twice output problem Faruk 8 4,436 Dec-26-2018, 11:57 AM
Last Post: Faruk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020