如何在python 3中有效管理多个URL响应



背景

我有一个针对一系列URL响应对象(json(运行分析的脚本。我通过迭代包含URL的字典来实现这一点,然后动态创建文件名并将这些文件写入磁盘,在将文件打开到内存时执行分析。

我想消除脚本中的IO,并将其向下推送给我的用户,他们可以使用panda数据帧在Jupyter笔记本中自己运行它来进行表格演示/逻辑

这是我不确定如何修改的代码片段:

for key, value in url_dict.items():
print("Issuing query for {}".format(key))
json_response = s.get(value, verify=cert_authority)
data = json_response.json()
jsonfilename = 'query_' + key + '.json'
jsonfile = os.path.join(query_output_directory, jsonfilename)
with open(jsonfile, 'wb') as outfile:
json.dump(data, outfile)

我试图弄清楚的是如何将各种json响应对象管道传输到它们自己的变量中;例如,我需要data1data2等,而不仅仅是data。感觉我需要动态变量,但我相信肯定有一个不那么棘手的解决方案。

您可以在循环时将响应附加到列表中。

alljsonvariables=[]
for key, value in url_dict.items():
print("Issuing query for {}".format(key))
json_response = s.get(value, verify=cert_authority)
alljsonvariables.append(json_response.json())
#the rest of your code goes here