bramwell2010 bramwell2010 - 1 month ago 7
JSON Question

python error parsing json

I'm trying to parse the information found in this link here: http://stats.nba.com/stats/playergamelog?DateFrom=&DateTo=&LeagueID=00&PlayerID=203518&Season=2016-17&SeasonType=Pre+Season

What I want to pull is the information under rowSet (so 0, 1, 2, etc will be one entry) enter image description here

Code I'm using:

import requests
urlPlayerLog = "http://stats.nba.com/stats/playergamelog?DateFrom=&DateTo=&LeagueID=00&PlayerID=203518&Season=2016-17&SeasonType=Pre+Season"
responses = requests.get(urlPlayerLog)
dataGameLogs = responses.json()['resultSets'][0]['rowSet']


This was working for me for months and then one day I kept getting the following error:
enter image description here

Which made me think the issue was with dataGameLogs = responses.json()['resultSets'][0]['rowSet'] , but unsure why that is returning an error...

Ken Ken
Answer

The webpage might be expecting a web browser to request the data from the URL. Try adding a user-agent to your request.

import requests
HEADERS = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/31.0'}
urlPlayerLog = "http://stats.nba.com/stats/playergamelog?DateFrom=&DateTo=&LeagueID=00&PlayerID=203518&Season=2016-17&SeasonType=Pre+Season"
responses = requests.get(urlPlayerLog, headers=HEADERS)
dataGameLogs = responses.json()['resultSets'][0]['rowSet']