json.loads失败,并显示JSONDecodeError:预期值

时间:2019-03-31 15:38:26

标签: python python-3.x python-requests

我正在尝试使用以下代码从Yahoo网站获取信息:

import json, requests

response = requests.get("https://finance.yahoo.com/quote/AAPL/options?p=AAPL")
output = json.loads(response.text)
print(output)

但是我收到此错误消息:

File "C:/Users/ziggy/.spyder-py3/untitled4.py", line 4, in <module>
    output = json.loads(response.text)

  File "C:\Users\ziggy\Anaconda3\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)

  File "C:\Users\ziggy\Anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())

  File "C:\Users\ziggy\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value

1 个答案:

答案 0 :(得分:0)

考虑到响应是HTML页面,真的不清楚您要在此处实现什么。

但是,如果我们只是在谈论摆脱错误,那么可以忽略它,首先使用json.dumps序列化响应:

import json, requests

response = requests.get("https://finance.yahoo.com/quote/AAPL/options?p=AAPL")
encoded_response = json.dumps(response.text)
output = json.loads(encoded_response)
print(output)