Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';正确格式化API json输出_Python_Json_Api_For Loop_Python Requests Html - Fatal编程技术网

Python Can';正确格式化API json输出

Python Can';正确格式化API json输出,python,json,api,for-loop,python-requests-html,Python,Json,Api,For Loop,Python Requests Html,输入: 输出: import requests import json apikey2 = '1234' headers = {'API-Key':apikey2,'Content-Type':'application/json'} data = {"url":urlz2, "visibility": "private"} r2 = requests.post('https://urlscan.io/api/v1/scan/',head

输入:

输出:

import requests
import json
apikey2 = '1234'
headers = {'API-Key':apikey2,'Content-Type':'application/json'}
data = {"url":urlz2, "visibility": "private"}
r2 = requests.post('https://urlscan.io/api/v1/scan/',headers=headers, data=json.dumps(data))
print(r2)
print(r2.json())
我尝试了一些类似于

message: Submission successful
uuid: xxxxxxxx
result: https://urlscan.io/result/xxxxxxxxxx/
visibility: private
但是get有一个错误,它是一个字符串,
response.json()
方法返回一个字典。您可以迭代此字典的键和值并打印

for match in r2.json().get('message', []):
    print(f'uuid: {message.get("uuid", {}).get("uuid", "Unknown uuid")}')

如果要打印特定键的值,如果找不到该键,则会显示一条消息,可以使用下面的代码。这段代码迭代感兴趣的键,如果该键不在响应字典中,则可以打印消息。否则,打印键和值

import requests
import json
apikey2 = '1234'
headers = {'API-Key':apikey2,'Content-Type':'application/json'}
data = {"url":urlz2, "visibility": "private"}
r2 = requests.post('https://urlscan.io/api/v1/scan/',headers=headers, data=json.dumps(data))
print(r2)
dictionary = r2.json()

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

非常感谢!出于某种原因,这项任务比我在工具im building lol上的第一个api的输出更重要
import requests
import json
apikey2 = '1234'
headers = {'API-Key':apikey2,'Content-Type':'application/json'}
data = {"url":urlz2, "visibility": "private"}
r2 = requests.post('https://urlscan.io/api/v1/scan/',headers=headers, data=json.dumps(data))
print(r2)
dictionary = r2.json()

for key, value in dictionary.items():
    print(f"{key} = {value}")
keys = ["message", "uuid", "result", "visibility"]
response_dict = r2.json()
for key in keys:
    value = response_dict.get(key, None)
    if value is None:
        print(f"{key} = Unknown {key}")
    else:
        print(f"{key} = {value}")