【问题标题】:"No JSON Object Could be decoded" when loading JSON data from API从 API 加载 JSON 数据时“无法解码任何 JSON 对象”
【发布时间】:2019-04-26 12:47:12
【问题描述】:

我尝试使用 Python json.loads 命令从 API 加载 JSON 数据,但收到错误消息“无法解码 JSON 对象”。

这是我尝试加载的 JSON API 数据:https://financialmodelingprep.com/api/financials/income-statement/AAPL

API 调用成功,当我执行“打印(response.content)”时,它会正确打印。但是,当我使用 json.loads 将 response.content 放入变量时,它会给出错误消息。

在下面的代码中,第一个 Print 运行良好。第二个 Print 给出错误消息。

这是我的代码:

import json
import requests


response = requests.get("https://financialmodelingprep.com/api/financials/income-statement/AAPL")

print (response.content)

data = json.loads(response.content)

print (data)

【问题讨论】:

    标签: json python-3.x python-2.7


    【解决方案1】:
    import json
    import requests
    
    response = 
    requests.get("https://financialmodelingprep.com/api/financials/income- 
    statement/AAPL")
    data = json.loads(response.text.replace("<pre>","").replace("</pre>",""))
    
    print (data)
    

    如果你想要更优雅的东西,你可以使用https://html.python-requests.org/

    【讨论】:

    • 非常感谢。我不太明白你做了什么,但它有效;-)
    • 请求的结果实际上是 HTML,而不是 json。所以我剥离了 HTML,然后将其输入 JSON 加载方法
    猜你喜欢
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 2015-01-04
    相关资源
    最近更新 更多