python - Creating dataframe from json not always working -
i'm trying run code create data frame json link. sometimes, code run. other times, error message (below). i'm not sure why occurs, though code same.
import requests import json url = "http://stats.nba.com/stats/leaguedashplayerstats?college=&conference=&country=&datefrom=&dateto=&division=&draftpick=&draftyear=&gamescope=&gamesegment=&height=&lastngames=0&leagueid=00&location=&measuretype=advanced&month=0&opponentteamid=0&outcome=&poround=0&paceadjust=n&permode=totals&period=0&playerexperience=&playerposition=&plusminus=n&rank=n&season=2016-17&seasonsegment=&seasontype=regular+season&shotclockrange=&starterbench=&teamid=0&vsconference=&vsdivision=&weight=" jd = requests.get(url).json() df = [] item in requests.get(url).json()['resultsets']: print("got here") row_df = [] row in item['rowset']: row_df.append(str(row).strip('[]')) df.append("\n") df.append(row_df) print(df)
error message:
traceback (most recent call last): file "/users/k/pycharmprojects/mousefun/fun", line 8, in <module> jd = requests.get(url).json() file "/library/frameworks/python.framework/versions/3.4/lib/python3.4/site-packages/requests/models.py", line 812, in json return complexjson.loads(self.text, **kwargs) file "/library/frameworks/python.framework/versions/3.4/lib/python3.4/json/__init__.py", line 318, in loads return _default_decoder.decode(s) file "/library/frameworks/python.framework/versions/3.4/lib/python3.4/json/decoder.py", line 343, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) file "/library/frameworks/python.framework/versions/3.4/lib/python3.4/json/decoder.py", line 361, in raw_decode raise valueerror(errmsg("expecting value", s, err.value)) none valueerror: expecting value: line 1 column 1 (char 0)
change request logic , try again:
r = requests.get(url) r.raise_for_status() df = [] item in r.json()["resultsets"]: # ...
r.raise_for_status()
raise if status not ok
.
also, not request 2 times code does.
Comments
Post a Comment