简体   繁体   English

Python - 尝试以某种方式从 API 结果打印 json 数据

[英]Python - Trying to print json data from API results in a certain way

Right now I have:现在我有:

import json

api_key = '123456'
url = "https://safebrowsing.googleapis.com/v4/threatMatches:find"
payload = {'client': {'clientId': "mycompany", 'clientVersion': "0.1"},
           'threatInfo': {'threatTypes': ["SOCIAL_ENGINEERING", "MALWARE"],
                          'platformTypes': ["ANY_PLATFORM"],
                          'threatEntryTypes': ["URL"],
                          'threatEntries': [{'url': "http://malware.testing.google.test/testing/malware/"}]}}
params = {'key': api_key}
r = requests.post(url, params=params, json=payload)
# Print response
print(r)
print(r.json())

Which gives me the results of:这给了我以下结果:

<Response [200]>
{'matches': [{'threatType': 'MALWARE', 'platformType': 'ANY_PLATFORM', 'threat': {'url': 'http://malware.testing.google.test/testing/malware/'}, 'cacheDuration': '300s', 'threatEntryType': 'URL'}]}

I want to print it more nicely / drop some data so it looks like:我想更好地打印它/删除一些数据,使其看起来像:

Url: 'http://malware.testing.google.test/testing/malware/'
ThreatType: 'MALWARE'
PlatformType: 'ANY_PLATFORM'

But every time I try something I just keep getting positional argument errors但是每次我尝试一些东西时,我都会不断收到位置参数错误

Edit: Having more than 2 urls submitted gives the following output编辑:提交超过 2 个 url 会提供以下输出

{'matches': [{'threatType': 'MALWARE', 'platformType': 'ANY_PLATFORM', 'threat': {'url': 'http://malware.testing.google.test/testing/malware/'}, 'cacheDuration': '300s', 'threatEntryType': 'URL'}, {'threatType': 'MALWARE', 'platformType': 'ANY_PLATFORM', 'threat': {'url': 'http://malware.testing.google.test/testing/malware/'}, 'cacheDuration': '300s', 'threatEntryType': 'URL'}]}

A defensive approach:防御方法:

for match in r.json().get('matches', []):
    print(f'URL: {match.get("threat", {}).get("url", "Unknown URL")}')
    print(f'threatType: {match.get("threatType", "Unknown ThreadType")}')
    print(f'platformType: {match.get("platformType", "Unknown PlatformType")}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用Python打印JSON数据 - Trying to print JSON data using Python 有没有办法在python中只打印带有json键的数据? - Is there a way to print only the data with the json key in python? 有没有办法在python中打印出JSON文件的某些元素? - Is there a way to print out certain elements of the JSON file in python? 如何将 JSON API 打印到 Python 中的数据帧 - How to print a JSON API to data frames in Python Python 和 Json - 从 API 中提取数据部分的最佳方法? - Python and Json - Best way to pull out sections of data from API? python 中的 discord bot 从 Z0ECD11C1D7A287401D148A23BBD7A2F 中的 API 中查找结果 - discord bot in python to lookup results form API in JSON and print 使用 Python 将 JSON 数据从 API 发送到 Firestore 的最佳方式 - Best way to send JSON data from API to Firestore with Python Python尝试将结果打印为菜单 - Python trying to print results as menu 尝试使用 python 从 API 请求中解析 JSON - Trying to parse JSON from API request with python JSON 文件解析错误,同时尝试使用 Python 打印某些值(json.decoder.JSONDecodeError:额外数据:第 2 行第 1 列(字符 20)) - JSON files parsing error while trying to print certain values using Python (json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 20))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM