我是 Python 和 API 的新手。我有一个 API 在工作,我可以访问它并获取计算机的详细信息。
在这一点上,我只想尝试一个非常基本的练习并打印计算机名和 IP 地址。
使用一本 Python 书,我有一个关于 API 的章节,我正在尝试以下作为测试并获得 200 个响应并且长度结果 = 10。所以初始连接正在工作。
import requests
# Make an API call and store response
url = 'https://removed.jamfcloud.com/JSSResource/advancedcomputersearches/id/8'
headers = {
'accept': 'application/json',
}
r = requests.get(url, headers=headers, auth=('Username','Password'))
print(f"Status code: {r.status_code}")
#Store API response in a variable
response_dict = r.json()
#Process Results
print(response_dict.keys())
repo_dicts = response_dict['advanced_computer_search']
print(f"Length of repo_dicts is: {len(repo_dicts)}")
此时我只想从结果中获取 1 条记录并显示计算机名称和 IP。
值得一提的是,这本书的示例使用了一些 GitHub API,但我正在修改它以使用我自己的 API。
书上说要添加以下代码:
#Examine the first repository
repo_dict = repo_dicts[0]
print(f"Computer Name: {repo_dict['Computer Name']}")
失败并说:
回溯(最近一次调用最后一次):
文件“...\API_Test_Jamf_1.py”,第 22 行,在
repo_dict = repo_dicts [0]
KeyError:0
这可能是有史以来最基本的事情,但我被困住了。
Web 浏览器中的 API 结果如下所示:
<advanced_computer_search>
<id>8</id>
<name>Test Pull</name>
<view_as>Standard Web Page</view_as>
<sort_1/>
<sort_2/>
<sort_3/>
<criteria>
<size>0</size>
</criteria>
<display_fields>
<size>102</size>
<display_field>
<name>Last Check-in</name>
</display_field>
<display_field>
<name>JSS Computer ID</name>
</display_field>
<display_field>
<name>Computer Name</name>
</display_field>
<display_field>
<name>IP Addresses</name>
</display_field>
接下来我需要做什么才能得到:
首先记录并显示计算机名和IP
所有记录都显示相同的 2 个字段?
慕婉清6462132
相关分类