试图将requests.get对象转换为json,结果出现错误



我是python和编程的新手,请耐心等待。

因此,我从API中提取了一个请求,但当我尝试转换为json时,我得到了以下错误

File "Transport Finder.py", line 44, in <module>
routejson = route.json()
File "C:ProgramDataAnaconda3libsite-packagesrequestsmodels.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "C:ProgramDataAnaconda3libjson__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:ProgramDataAnaconda3libjsondecoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:ProgramDataAnaconda3libjsondecoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这是我目前使用的代码,以前是有效的,但我刚刚回到它,现在得到了这个错误。

route = requests.get(f'http://router.project-osrm.org/route/v1/driving/{StartLL};{EndLL}?alternatives=false&annotations=nodes')
routejson = route.json()

根据错误输出,您得到的是一个空响应。也许url有问题,或者您需要在请求的同时传递一些头。

您可以print(route.text)调试请求的输出,然后在获得满意的输出后对其进行解析。

最新更新