要保存JSON响应n分钟,可以使用以下代码示例:
import requests
import json
import time
# 发送请求
response = requests.get('https://api.example.com/data')
# 将响应保存为JSON文件
with open('response.json', 'w') as file:
file.write(response.text)
import requests
import json
import time
# 保存JSON响应的函数
def save_response():
# 发送请求
response = requests.get('https://api.example.com/data')
# 将响应保存为JSON文件
with open('response.json', 'w') as file:
file.write(response.text)
# 设置保存间隔时间(以分钟为单位)
save_interval = 5
# 循环执行保存操作
while True:
save_response()
time.sleep(save_interval * 60) # 将分钟转换为秒钟
在上述示例中,首先使用requests库发送一个请求并获取响应。然后,将响应保存为一个JSON文件。最后,使用一个无限循环和定时器来定期执行保存操作,间隔时间为n分钟。可以根据需要修改保存的文件名、请求的URL以及保存的时间间隔。
上一篇:保存json文件的困难
下一篇:保存JSON字符串列表的最佳格式