如何使用API​​和Python提取数据?

时间:2019-10-29 12:09:08

标签: python json api loops

我有很多服务器,我使用API​​提取有关存储的数据。 要获取有关存储uuid (以json命名的存储)的详细信息,我使用以下代码:

srvdet = requests.get('https://hhh.com/1.2/server/76856585', auth=HTTPBasicAuth('login', 'pass'))
srvdet_json = srvdet.json()
datas = srvdet.json()
print(datas)

并生成json:

{
   "server": {
      "boot_order": "",
      "core_number": "2",
      "created": ,
      "firewall": "off",
      "host": ,
      "hostname": "hello",
      "ip_addresses": {
         "ip_address": [
            {
               "access": "private",
               "address": "",
               "family": ""
            },
            {
               "access": "",
               "address": "",
               "family": "",
               "part_of_plan": ""
            }
         ]
      },
      "license": 0,
      "memory_amount": "",
      "nic_model": "",
      "plan": "",
      "plan_ipv4_bytes": "",
      "plan_ipv6_bytes": "0",
      "state": "started",
      "storage_devices": {
         "storage_device": [
            {
               "address": "",
               "boot_disk": "0",
               "part_of_plan": "",
               "storage": "09845",
               "storage_size": ,
               "storage_title": "",
               "type": ""

}

目前,它工作正常。问题是当我需要获取“ 09845” 时,这对于存储是很重要的。当我尝试使用此代码时:

for storage in datas['server']['storage_devices']['storage_device']:
    srvstorage = storage
    print(srvstorage)

结果是:

{'storage': '09845', 'storage_size':, 'address': '', 'boot_disk': '0', 'type': '', 'part_of_plan': 'yes', 'storage_title': ''}

我在做什么错?如何在变量中保存“ 09845”?

编辑:

现在,尝试访问有关存储的详细信息时出现错误。 我想提取json中的状态的备份状态:

{
   "storage": {
      "access": "private",
      "backup_rule": {},
      "backups": {
         "backup": []
      },
      "license": 0,
      "part_of_plan": "",
      "servers": {
         "server": [
            ""
         ]
      },
      "size": ,
      "state": "online",
      "tier": "",
      "title": "",
      "type": "",
      "uuid": "",
      "zone": ""
   }
}

执行此代码时:

bkpdet = requests.get('https://fffff.com/1.2/storage/08475', auth=HTTPBasicAuth('login', 'pass'))
bkpdet_json = bkpdet.json()
datastg = bkpdet.json()
print(datastg)
for sts in datastg['storage']:
    bkpsts = sts['state']
    print(bkpsts)

我收到此错误:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: string indices must be integers

整个想法是最后使用以下代码获取有关状态的信息:

if bkpsts == "online":
    print('Backup has been created.')
else bkpsts == "backuping":
    print('Backup creation is in progress.')
else:
    print(bkpdet.status_code)

我在搜索,但仍然找不到这里的问题。

2 个答案:

答案 0 :(得分:2)

当您想访问存储时,您会错过一个键,您可以遍历该键,这很好。但是在每次迭代中,您都会得到一个字典,您需要从字典中调用正确的键,对于您的情况,storage

for storage in datas['server']['storage_devices']['storage_device']: 
    srvstorage = storage.get("storage", None) 
    print(srvstorage)

注意

最好使用

get方法,因为您可能会遇到设备内部没有存储信息的设备,通过使用get可以避免使用KeyError

答案 1 :(得分:1)

您可以做

for device in datas['server']['storage_devices']['storage_device']:
    device_storage = device['storage']
    print(device_storage)