【问题标题】:Trying to convert a requests.get object to a json and getting an error尝试将 requests.get 对象转换为 json 并收到错误
【发布时间】:2021-05-26 14:46:08
【问题描述】:

我是 python 和一般编程的新手,所以请耐心等待..

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

File "Transport Finder.py", line 44, in <module>
    routejson = route.json()
  File "C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\ProgramData\Anaconda3\lib\json\decoder.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()

【问题讨论】:

    标签: python json python-requests


    【解决方案1】:

    根据错误输出,您得到一个空响应。 可能 url 有问题,或者您需要在请求中传递一些标头。

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

    【讨论】:

    • 就是这样,我在该死的变量中有一个差距,d'oh。
    猜你喜欢
    • 2017-04-27
    • 2019-05-28
    • 2022-06-13
    • 2017-06-16
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    相关资源
    最近更新 更多