2017-05-29 25 views
0

我有大約100臺運行Mersive Solstice的機器,這是一個無線顯示工具。我試圖收集一些重要的信息,特別是每個安裝實例的許可證的履行ID。Mersive Solstive API:AttributeError:'dict'對象沒有屬性'm_displayInformation'

使用Solstice OpenControl API,發現here,我掀起了一個python腳本來抓取我需要的所有東西,使用json GET。但是,使用的例子,即使從文檔GET,

import requests 
import json 
url = ‘http://ip-of-machine/api/stats’ 

r = requests.get(url) 
jsonStats = json.loads(r.text) 
usersConnected = jsonStats.m_statistics.m_connectedUsers 

我遇到:

Traceback (most recent call last): 
    File "C:/Python27/test.py", line 7, in <module> 
    usersConnected = jsonStats.m_statistics.m_connectedUsers 
AttributeError: 'dict' object has no attribute 'm_statistics' 

這是非常混亂。關於這個問題,我在SO上發現了很多類似的問題,但沒有一個專門針對來自API參考指南的錯誤GET請求。

此外,這裏是我的腳本:

import requests 
import json 
from time import sleep 

url = 'test' 

f = open("ip.txt", "r") 
while(url != ""): 
    url = f.readline() 
    url = url.rstrip('\n') 
    print(url) 

    try: 
     r = requests.get(url) 
    except: 
     sleep(5) 

    jsonConfig = json.loads(r.text) 
    displayName = jsonConfig.m_displayInformation.m_displayName 
    hostName = jsonConfig.m_displayInformation.m_hostName 
    ipv4 = jsonConfig.m_displayInformation.m_ipv4 
    fulfillmentId = jsonConfig.m_licenseCuration.fulfillmentId 
    r.close() 


f.close 

我導入的URL從易飼養文本文檔。我能夠連接到/ api/config JSON,並且當URL被放入瀏覽器時,它會吐出JSON記錄:

回答

1

Json使用「Dicts」,它是一種數組類型。你只是以錯誤的方式使用它們。我推薦閱讀Python Data Structures.

Json.Loads() 

返回字典而不是對象。這樣做:

dict['key']['key'] 

這裏是你的代碼應該是什麼樣子:

import requests 
import json 
from time import sleep 

url = 'test' 

f = open("ip.txt", "r") 

while(url != ""): 

    url = f.readline() 
    url = url.rstrip('\n') 
    print(url) 

    try: 
     response = requests.get(url) 
     json_object = json.loads(response .text) 
     displayName = json_object['m_displayInformation']['m_displayName'] 
     hostName = json_object['m_displayInformation']['m_hostName'] 
     ipv4 = json_object['m_displayInformation']['m_ipv4'] 
     fulfillmentId = json_object['m_licenseCuration']['fulfillmentId'] 
    except: 
     pass 

    response .close() 
f.close() 

我希望這是有幫助!

+0

大約5分鐘前,我決定將我的符號從點切換到括號,這當然有訣竅。我回到自己的答案,但是你描述的問題比我的要好得多!乾杯! – Dupontrocks11