【问题标题】:Saving multiple requests in a single JSON在单个 JSON 中保存多个请求
【发布时间】:2019-03-09 06:14:22
【问题描述】:

我的问题如下:我有一个在 nparray (ip_array) 中排序的 Ips 列表,然后我想对所有这些请求执行多个请求并将输出保存在单个 json 中。 (APIKEY其实就是代码xD中的api key)

url_auth = 'https://api.ipgeolocation.io/ipgeo?apiKey=APIKEYAPIKEYAPIKEY='
for i in np.arange(1,4): 
    r[i] = requests.request(method='get',url=url_auth,params={'ips':ip_array[i]}) #i tested the single request and it works in this way.

然后,我明白了

TypeError: 'Response' object does not support item assignment

然后,我尝试用

替换最后一行
 r = requests.request(method='get',url=url_auth,params={'ips':ip_array[i]})

但是,当我这样做时

r.json()

我只收到最后一个请求(这很明显)。

【问题讨论】:

    标签: python json api python-requests jupyter


    【解决方案1】:

    在每次迭代时存储响应:

    url_auth = 'https://api.ipgeolocation.io/ipgeo?apiKey=APIKEYAPIKEYAPIKEY='
    responses = []
    for i in np.arange(1,4): 
        response = requests.request(method='get',url=url_auth,params={'ips':ip_array[i]})
        responses.append(response.json())
    

    responses 列表将包含所有响应对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      相关资源
      最近更新 更多