使用 Python 打印嵌套 Json API 数据
Printing Nested Json API data using Python
我能够使用 API get 请求接收数据,现在我只需要帮助打印一些对象。我遇到了麻烦,因为我需要的对象嵌套得很深。我需要的对象:
-cve ID
-url 参考
-描述
-严重性
json 页面:https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2021-40463/
import requests
import json
import pprint
url = "https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2021-40463/"
params = {"q": "CVE"}
response = requests.get(url, params)
data = json.loads(response.text)
pprint.pprint (data)
import requests
import json
import pprint
url = "https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2021-40463/"
params = {"q": "CVE"}
response = requests.get(url, params)
data = json.loads(response.content)
pprint.pprint(data)
response.content会return回复的内容。之后:
cve ID:pprint.pprint(data['result']['CVE_Items'][0]['cve']['CVE_data_meta']['ID'])
url 参考:pprint.pprint(data['result']['CVE_Items'][0]['cve']['references']['reference_data'][0]['url'])
描述:pprint.pprint(data['result']['CVE_Items'][0]['cve']['description']['description_data'][0]['value'])
严重性:pprint.pprint(data['result']['CVE_Items'][0]['impact']['baseMetricV2']['severity'])
我能够使用 API get 请求接收数据,现在我只需要帮助打印一些对象。我遇到了麻烦,因为我需要的对象嵌套得很深。我需要的对象:
-cve ID -url 参考 -描述 -严重性
json 页面:https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2021-40463/
import requests
import json
import pprint
url = "https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2021-40463/"
params = {"q": "CVE"}
response = requests.get(url, params)
data = json.loads(response.text)
pprint.pprint (data)
import requests
import json
import pprint
url = "https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2021-40463/"
params = {"q": "CVE"}
response = requests.get(url, params)
data = json.loads(response.content)
pprint.pprint(data)
response.content会return回复的内容。之后:
cve ID:pprint.pprint(data['result']['CVE_Items'][0]['cve']['CVE_data_meta']['ID'])
url 参考:pprint.pprint(data['result']['CVE_Items'][0]['cve']['references']['reference_data'][0]['url'])
描述:pprint.pprint(data['result']['CVE_Items'][0]['cve']['description']['description_data'][0]['value'])
严重性:pprint.pprint(data['result']['CVE_Items'][0]['impact']['baseMetricV2']['severity'])