【问题标题】:Looping through GET request with multiple parameters of different length使用多个不同长度的参数循环 GET 请求
【发布时间】:2020-02-17 12:25:19
【问题描述】:

追问this question我几天前问的:

我仍然需要为列表中的每个元素调用 API,但现在我需要一些额外的参数。我有一个不同长度的每个参数的值列表。每个customerId 都有多个keywordIds 和areaIds(对于给定的客户,它们总是具有相同的长度)。 每个客户都有不同的关键字。例如,前五个关键字 ID 属于 customerlist 中的第一个 ID。接下来的三个关键字 ID 到 customerlist 中的第二个 ID,依此类推。 我用zip 尝试过,但我只得到第一个客户ID 的第一个关键字的第一个条目,而不是每个客户的每个关键字的所有条目(在这种情况下,关键字1172646 和来自客户的区域29010 的信息803818 其中 isareafirst = false)。

import json
import requests
import itertools

API_BASEURL = "https://exampleurl.com/"
API_TOKEN = "abc"
HEADERS = {'content-type' : 'application/json',
           'Authorization': API_TOKEN }


customerlist = [803818, 803808, 803803,803738,803730]
keywordlist = [1172646, 1218994,1218992 1218993,1218992, 1218995, 1218993, 1173529, 1235569,1187456,1187455,1187453]
arealist = [29010, 28882, 28882, 28882, 28882, 28882, 28882, 28882, 31237,31237, 31237,31237]
isareafirstlist = [False, False, True, False, False, False, True, False, False,False, False, False]



def get_history(endpoint):
        responses = []
        for i,j,k,l in zip(customerlist,keywordlist, arealist, isareafirstlist):
            api_endpoint = endpoint
            params = {'customerid' : i,
                      'keywordid' : j,
                      'areaid' : k,
                      'isareafirst': l}
            response = requests.get(f"{API_BASEURL}/{api_endpoint}",
                                 params = params,
                                 headers = HEADERS)
            res = json.loads(response.text)
            responses.append(res)
        return (responses)

我也尝试了itertools.zip_longest,但这只会给我错误,我收到带有zip 的空列表

def get_history(endpoint):
    responses = []
    for i,j,k,l in itertools.zip_longest(customerlist,keywordlist, arealist, isareafirstlist):
        api_endpoint = endpoint
        params = {'customerid' : i,
                  'keywordid' : j,
                  'areaid' : k,
                  'isareafirst': l}
        response = requests.get(f"{API_BASEURL}/{api_endpoint}",
                             params = params,
                             headers = HEADERS)
        res = json.loads(response.text)
        responses.append(res)
    return (responses)

那么,我怎样才能在 API 中循环使用所有可能的组合呢?

【问题讨论】:

  • 试试list.itertools.product(*list_of_lists),其中list_of_lists[customerlist, keywordlist, arealist, isareafirstlist]的列表。

标签: python get-request


【解决方案1】:

这个实现会有帮助吗?

import itertools
customerlist = [803818, 803808, 803803,803738,803730]
keywordlist = [1172646, 1218994,1218992, 1218993,1218992, 1218995, 1218993, 1173529, 1235569,1187456,1187455,1187453]
arealist = [29010, 28882, 28882, 28882, 28882, 28882, 28882, 28882, 31237,31237, 31237,31237]
isareafirstlist = [False, False, True, False, False, False, True, False, False,False, False]
list_of_lists = [customerlist, keywordlist, arealist, isareafirstlist]
for _ in itertools.product(*list_of_lists):
    print _

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2019-08-05
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多