Python 3.5: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Python 3.5: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我有这段Python代码:

homedir = '/home/cloudera/'
baseurl = 'http://ergast.com/api/f1/'

apilist = ['seasons', 'results', 'qualifying', 'driverStandings', 'constructorStandings', 'drivers', 'constructors',
           'circuits', 'status', 'laps', 'pitstops']

for 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) for y, a in itertools.product(range(1950, 2016), apilist)]
files = [parent_fname.format(yf=y, api=a) for y, a in itertools.product(range(1950, 2016), apilist)]


for url, file in zip(urls, files):
    print(url, file)
    response = requests.get(url).json()
    with open(file, 'w') as f:
       json.dump(response, f)

当我运行这个时,这是我得到的输出:

/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) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Process finished with exit code 1

基本上,它会为 1950 年创建 JSON 文件,但下一次循环 运行s 为 1951 年时,它会退出。我是 Python 的新手,也是 JSON 的新手。有人能弄清楚这是怎么回事吗?

注意:该程序的一个不太复杂的版本,包含从 1950 年到 2015 年的所有年份,但只对 qualifying 有效。我试图扩大规模并将所有属性放在一起。该版本仅使用一个范围来遍历年份并生成文件名和 url。

出现这个问题是因为url:http://ergast.com/api/f1/1950/laps.json?limit=100000 returns a error. Look the url:http://ergast.com/api/f1/1950/status.json?limit=100000 (Image Above) 此图片具有有效的 json 格式,此页面的来源是:

url:http://ergast.com/api/f1/1950/laps.json?limit=100000 return a html error:

而这个 url 的来源是 html table 而不是 json 格式:

可以使用错误处理来抑制此错误,但无法解析格式。可能需要 url 到 return 中的一些其他参数来获得正确的值。 我这个 link: http://ergast.com/mrd/ 有这个 api 的文档,在 Overview 部分说:

如果您尝试在年份后将数值放入 url,例如:http://ergast.com/api/f1/1950/1/laps.json?limit=100000 you return some valid results, like this image: