我有一个站点,只有JSON数据。我需要用python读取这些数据。
所以我需要知道,我如何才能加载一个在线JSON。
import json
f = "https://api.npoint.io/7872500d7eef44a03194"
data = json.load(f)
那么这是怎么回事呢?然后我如何检查互联网连接错误?
使用requests
库:
import requests
f = "https://api.npoint.io/7872500d7eef44a03194"
data = requests.get(f).json()
data
输出:
{'sample': 'this is only a sample'}
您可以使用下面的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'}