首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python -尝试以某种方式打印API中的json数据。

Python -尝试以某种方式打印API中的json数据。
EN

Stack Overflow用户
提问于 2020-10-12 02:20:30
回答 1查看 28关注 0票数 0

现在我有:

代码语言:javascript
复制
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())

这给了我以下的结果:

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

我想更好地打印它/删除一些数据,这样看起来如下:

代码语言:javascript
复制
Url: 'http://malware.testing.google.test/testing/malware/'
ThreatType: 'MALWARE'
PlatformType: 'ANY_PLATFORM'

但每次我尝试的时候,我总是会有位置上的错误

编辑:提交两个以上的urls将提供以下输出

代码语言:javascript
复制
{'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'}]}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-12 02:33:35

防御性方法:

代码语言:javascript
复制
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")}')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64310881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档