Python和Json从API输出打印出键/值并对其进行结构化

2024-05-26 00:33:42 发布

您现在位置:Python中文网/ 问答频道 /正文

输入:

data = {'data': {'requests': [], 'cookies': [], 'console': [], 'links': [], 'timing': {'beginNavigation': '2020-10-13T13:29:48.999Z', 'frameStartedLoading': '2020-10-13T13:29:49.889Z', 'frameNavigated': '2020-10-13T13:29:49.899Z', 'loadEventFired': '2020-10-13T13:29:49.899Z', 'frameStoppedLoading': '2020-10-13T13:29:49.899Z', 'domContentEventFired': '2020-10-13T13:29:49.899Z'}, 'globals': []}, 'stats': {'resourceStats': [], 'protocolStats': [], 'tlsStats': [], 'serverStats': [], 'domainStats': [], 'regDomainStats': [], 'ipStats': []}, 'meta': {'processors': {'geoip': {'state': 'done', 'data': [{'ip': '162.0.232.249', 'geoip': {'range': [2717966336, 2717970431], 'country': 'CA', 'region': '', 'eu': '0', 'timezone': 'America/Toronto', 'city': '', 'll': [43.6319, -79.3716], 'metro': 0, 'area': 1000, 'country_name': 'Canada'}}]}, 'asn': {'state': 'done', 'data': [{'ip': '162.0.232.249', 'asn': '22612', 'country': 'US', 'registrar': 'arin', 'date': '2011-06-21', 'description': 'NAMECHEAP-NET, US', 'route': '162.0.232.0/24'}]}, 'rdns': {'state': 'done', 'data': [{'ip': '162.0.232.249', 'ptr': 'server290-1.web-hosting.com'}]}, 'wappa': {'state': 'done', 'data': []}, 'done': {'state': 'done', 'data': {'state': 'done'}}}}, 'task': {'uuid': '4f830f9c-57e3-4099-9840-44fa8e1f5f7c', 'time': '2020-10-13T13:29:48.871Z', 'url': 'http://billinginfo-netflix.com/', 'visibility': 'public', 'options': {'useragent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'}, 'method': 'api', 'source': 'c49ff09d', 'tags': [], 'reportURL': 'https://urlscan.io/result/4f830f9c-57e3-4099-9840-44fa8e1f5f7c/', 'screenshotURL': 'https://urlscan.io/screenshots/4f830f9c-57e3-4099-9840-44fa8e1f5f7c.png', 'domURL': 'https://urlscan.io/dom/4f830f9c-57e3-4099-9840-44fa8e1f5f7c/'}, 'page': {}, 'lists': {'ips': [], 'countries': [], 'asns': [], 'domains': [], 'servers': [], 'urls': [], 'hashes': []}, 'verdicts': {'overall': {'score': 0, 'categories': [], 'brands': [], 'tags': [], 'malicious': False, 'hasVerdicts': 0}, 'urlscan': {'score': 0, 'categories': [], 'brands': [], 'tags': [], 'detectionDetails': [], 'malicious': False}, 'engines': {'score': 0, 'malicious': [], 'benign': [], 'maliciousTotal': 0, 'benignTotal': 0, 'verdicts': [], 'enginesTotal': 0}, 'community': {'score': 0, 'votes': [], 'votesTotal': 0, 'votesMalicious': 0, 'votesBenign': 0, 'tags': [], 'categories': []}}}

for key, value in data.items():
    print(f"{key} = {value}")

我设法通过数据中的键将其分解:

输出:

data = {'requests': [], 'cookies': [], 'console': [], 'links': [], 'timing': {'beginNavigation': '2020-10-13T13:29:48.999Z', 'frameStartedLoading': '2020-10-13T13:29:49.889Z', 'frameNavigated': '2020-10-13T13:29:49.899Z', 'loadEventFired': '2020-10-13T13:29:49.899Z', 'frameStoppedLoading': '2020-10-13T13:29:49.899Z', 'domContentEventFired': '2020-10-13T13:29:49.899Z'}, 'globals': []}
stats = {'resourceStats': [], 'protocolStats': [], 'tlsStats': [], 'serverStats': [], 'domainStats': [], 'regDomainStats': [], 'ipStats': []}
meta = {'processors': {'geoip': {'state': 'done', 'data': [{'ip': '162.0.232.249', 'geoip': {'range': [2717966336, 2717970431], 'country': 'CA', 'region': '', 'eu': '0', 'timezone': 'America/Toronto', 'city': '', 'll': [43.6319, -79.3716], 'metro': 0, 'area': 1000, 'country_name': 'Canada'}}]}, 'asn': {'state': 'done', 'data': [{'ip': '162.0.232.249', 'asn': '22612', 'country': 'US', 'registrar': 'arin', 'date': '2011-06-21', 'description': 'NAMECHEAP-NET, US', 'route': '162.0.232.0/24'}]}, 'rdns': {'state': 'done', 'data': [{'ip': '162.0.232.249', 'ptr': 'server290-1.web-hosting.com'}]}, 'wappa': {'state': 'done', 'data': []}, 'done': {'state': 'done', 'data': {'state': 'done'}}}}
task = {'uuid': '4f830f9c-57e3-4099-9840-44fa8e1f5f7c', 'time': '2020-10-13T13:29:48.871Z', 'url': 'http://billinginfo-netflix.com/', 'visibility': 'public', 'options': {'useragent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'}, 'method': 'api', 'source': 'c49ff09d', 'tags': [], 'reportURL': 'https://urlscan.io/result/4f830f9c-57e3-4099-9840-44fa8e1f5f7c/', 'screenshotURL': 'https://urlscan.io/screenshots/4f830f9c-57e3-4099-9840-44fa8e1f5f7c.png', 'domURL': 'https://urlscan.io/dom/4f830f9c-57e3-4099-9840-44fa8e1f5f7c/'}
page = {}
lists = {'ips': [], 'countries': [], 'asns': [], 'domains': [], 'servers': [], 'urls': [], 'hashes': []}
verdicts = {'overall': {'score': 0, 'categories': [], 'brands': [], 'tags': [], 'malicious': False, 'hasVerdicts': 0}, 'urlscan': {'score': 0, 'categories': [], 'brands': [], 'tags': [], 'detectionDetails': [], 'malicious': False}, 'engines': {'score': 0, 'malicious': [], 'benign': [], 'maliciousTotal': 0, 'benignTotal': 0, 'verdicts': [], 'enginesTotal': 0}, 'community': {'score': 0, 'votes': [], 'votesTotal': 0, 'votesMalicious': 0, 'votesBenign': 0, 'tags': [], 'categories': []}}

如何从verdict中获取这些子值并打印如下内容:

Overall:
Score: 0
Malicious: false

为了去除大量不必要的数据


Tags: httpsioipdatatagscountrycategoriesus