【发布时间】:2021-05-18 04:28:19
【问题描述】:
尝试获取每个响应以保存。 到目前为止,它只打印所有响应并保存一个。 请帮忙。 编辑:需要帮助保存所有回复。
import requests
import json
url = 'https://www.virustotal.com/vtapi/v2/file/report'
resource = []
with open("/PATH/hashes.txt", "r") as f:
for resource in f:
print(resource)
params = {'apikey': 'API KEY HERE', 'resource': resource}
response = requests.get(url, params=params)
response1 = response.json()
print(response.json())
with open('data.json', 'w') as J:
json.dump(response1, J, indent=6)
更新!!感谢所有帮助!
import requests
import json
url = 'https://www.virustotal.com/vtapi/v2/file/report'
params = {'apikey': 'API KEY HERE'}
resources = []
with open("/PATH/hashes.txt", "r") as f:
for resource in f:
print(resource)
params['resource'] = resource
response = requests.get(url, params=params)
json_data = response.json()['sha256']
print(json_data)
resources.append(json_data)
with open('data.json', 'w') as J:
json.dump(resources, J, indent=6)
J.close()
f.close()
【问题讨论】:
-
在 for 循环的每次迭代中,您都会覆盖
data.json -
使用
with上下文管理器时不需要关闭文件。上下文管理器会处理它 -
另外,如果您只想从每个响应中获取一个字符串 (
sha256),您真的需要输出文件为 JSON 格式吗?
标签: python python-3.x get