【问题标题】:Python and Json - Printing out keys/values from API output and structuring itPython 和 Json - 从 API 输出中打印出键/值并对其进行结构化
【发布时间】:2020-10-14 02:11:54
【问题描述】:

输入:

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': []}}

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

Overall:
Score: 0
Malicious: false

为了去掉很多不必要的数据

【问题讨论】:

  • 我没有看到你的问题,我认为你需要一些关于字典的一般帮助 (docs.python.org/3/tutorial/datastructures.html#dictionaries)。输出中的元素也是字典。您可以访问 verdicsts["overall"]["score"] 之类的元素,也可以不使用输出 data["verdicsts"]["overall"]["score"]

标签: python json api key-value


【解决方案1】:

我能够弄明白,一直在尝试 .get[]。我猜只是我想把事情复杂化。

data['verdicts']['overall']['malicious']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2022-06-11
    • 2014-05-07
    • 2022-11-12
    • 1970-01-01
    • 2020-11-18
    • 2020-03-26
    相关资源
    最近更新 更多