Python 3.5: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) -
i have piece of python code:
homedir = '/home/cloudera/' baseurl = 'http://ergast.com/api/f1/' apilist = ['seasons', 'results', 'qualifying', 'driverstandings', 'constructorstandings', 'drivers', 'constructors', 'circuits', 'status', 'laps', 'pitstops'] dir in apilist: target = homedir + str(dir) try: shutil.rmtree(target, ignore_errors=true) os.makedirs(target) except: pass parent_url = baseurl + "{y}/{api}.json?limit=100000" parent_fname = homedir + "{api}/{yf}.json" urls = [parent_url.format(y=y, api=a) y, in itertools.product(range(1950, 2016), apilist)] files = [parent_fname.format(yf=y, api=a) y, in itertools.product(range(1950, 2016), apilist)] url, file in zip(urls, files): print(url, file) response = requests.get(url).json() open(file, 'w') f: json.dump(response, f)
when run this, output get:
/home/cloudera/pycharmprojects/ergastf1/ergastf1/bin/python /home/cloudera/pycharmprojects/ergastf1/f1demo.py http://ergast.com/api/f1/1950/seasons.json?limit=100000 /home/cloudera/seasons/1950.json http://ergast.com/api/f1/1950/results.json?limit=100000 /home/cloudera/results/1950.json http://ergast.com/api/f1/1950/qualifying.json?limit=100000 /home/cloudera/qualifying/1950.json http://ergast.com/api/f1/1950/driverstandings.json?limit=100000 /home/cloudera/driverstandings/1950.json http://ergast.com/api/f1/1950/constructorstandings.json?limit=100000 /home/cloudera/constructorstandings/1950.json http://ergast.com/api/f1/1950/drivers.json?limit=100000 /home/cloudera/drivers/1950.json http://ergast.com/api/f1/1950/constructors.json?limit=100000 /home/cloudera/constructors/1950.json http://ergast.com/api/f1/1950/circuits.json?limit=100000 /home/cloudera/circuits/1950.json http://ergast.com/api/f1/1950/status.json?limit=100000 /home/cloudera/status/1950.json http://ergast.com/api/f1/1950/laps.json?limit=100000 /home/cloudera/laps/1950.json traceback (most recent call last): file "/home/cloudera/pycharmprojects/ergastf1/f1demo.py", line 74, in <module> response = requests.get(url).json() file "/home/cloudera/pycharmprojects/ergastf1/ergastf1/lib/python3.5/site-packages/requests/models.py", line 812, in json return complexjson.loads(self.text, **kwargs) file "/usr/local/lib/python3.5/json/__init__.py", line 319, in loads return _default_decoder.decode(s) file "/usr/local/lib/python3.5/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) file "/usr/local/lib/python3.5/json/decoder.py", line 357, in raw_decode raise jsondecodeerror("expecting value", s, err.value) none json.decoder.jsondecodeerror: expecting value: line 1 column 1 (char 0) process finished exit code 1
basically, creates json files year 1950, next time loop runs year 1951, exits. i'm new python , newer still json. can figure out what's going on?
note: less complex version of program, years 1950 2015, qualifying
had worked. trying go large scale , attributes together. version merely used range iterate through years , generate file names , urls.
this problem raised because url: http://ergast.com/api/f1/1950/laps.json?limit=100000 returns error. url:http://ergast.com/api/f1/1950/status.json?limit=100000 (image above) image have working json format, , source page is:
the url:http://ergast.com/api/f1/1950/laps.json?limit=100000 return html error:
and source of url html table , not json format:
this error can suppresed using error treatment, cant resolve format. maybe need other argument in url return corret values. link: http://ergast.com/mrd/ have documentation of api, , in section overview says:
if try put numeric value url after year, : http://ergast.com/api/f1/1950/1/laps.json?limit=100000 return valid results, image:
Comments
Post a Comment