【问题标题】:How can i read JSON data from online file?(python) [duplicate]我如何从在线文件中读取 JSON 数据?(python)[重复]
【发布时间】:2021-08-19 13:25:10
【问题描述】:

我有一个网站,只有 JSON 数据。我需要用 python 读取这些数据。

所以我需要知道,如何加载在线 JSON。

import json

f = "https://api.npoint.io/7872500d7eef44a03194"

data = json.load(f)

那么这如何工作呢?那么如何检查互联网连接错误?

【问题讨论】:

    标签: python json api


    【解决方案1】:

    requests 库:

    import requests
    
    f = "https://api.npoint.io/7872500d7eef44a03194"
    data = requests.get(f).json()
    
    data
    

    输出:

    {'sample': 'this is only a sample'}
    

    【讨论】:

    • 最好是data = resp.json()
    • @buran 不确定它会好多更好,但是是的,它肯定会更简洁:) 更新了答案。谢谢!
    【解决方案2】:

    您可以在下面使用 API 调用

    import requests
    
    url = "https://api.npoint.io/7872500d7eef44a03194"
    response = requests.request('get', url)
    print(json.loads(response.text))
    

    输出

    {'sample': 'this is only a sample'}
    

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 2020-08-07
      • 1970-01-01
      相关资源
      最近更新 更多