拨打多个 API 电话 - Python
Make multiple API calls - Python
我经常使用 API 调用来提取一些客户数据。
但是,每当我尝试提取超过 20 个客户 ID 时,API 就会停止工作。
发生这种情况时,我 运行 多次 API 调用,将每个 JSON 输出转换为 df 并将所有数据帧附加在一起。
当我只需要几个 API 调用时这很好,但是当我有多个客户 ID 需要拉取时效率低下,因为有时我必须 运行 5/10 分开 API 来电。
我认为循环可以帮助解决这个问题。鉴于我对 Python 的经验很少,我查看了有关循环 APIs 的其他问题,但我找不到解决方案。
下面是我使用的代码。如何进行单个 API 循环调用多个客户 ID(请记住,每次调用有大约 20 个 ID 的限制)和 returns 单个数据帧?
谢谢!
#list of customer ids
customer_id = [
"1004rca402itas8470der874",
"1004rca402itas8470der875,
"1004rca402itas8470der876",
"1004rca402itas8470der877",
"1004rca402itas8470der878",
"1004rca402itas8470der879"
]
#API call
payload = {'customer':",".join(customer_id), 'countries':'DE, 'granularity':'daily', 'start_date':'2021-01-01', 'end_date':'2022-03-31'}
response = requests.get('https://api.xxxxxxjxjx.com/t3/customers/xxxxxxxxxxxx?auth_token=xxxxxxxxxxxx', params=payload)
response.status_code
#convert to dataframe
api = response.json()
df = pd.DataFrame(api)
df['sales'] = df['domestic_sales'] + df['international_sales']
df = df[['customer_id','country','date','sales']]
df.head()
大致思路如下:
# List of dataframes
dfs = []
# List of lists of 20 customer ids each
ids = [customer_id[i:i+20] for i in range(0, len(customer_id), 20)]
# Iterate on 'ids' to call api and store new df in list called 'dfs'
for chunk in ids:
payload = {
"customer": ",".join(chunk),
"countries": "DE",
"granularity": "daily",
"start_date": "2021-01-01",
"end_date": "2022-03-31",
}
response = requests.get(
"https://api.xxxxxxjxjx.com/t3/customers/xxxxxxxxxxxx?auth_token=xxxxxxxxxxxx",
params=payload,
)
dfs.append(pd.DataFrame(response.json()))
# Concat all dataframes
df = dfs[0]
for other_df in dfs[1:]:
df = pd.concat([df, other_df])
# Additional work
df['sales'] = df['domestic_sales'] + df['international_sales']
df = df[['customer_id','country','date','sales']]
我经常使用 API 调用来提取一些客户数据。 但是,每当我尝试提取超过 20 个客户 ID 时,API 就会停止工作。 发生这种情况时,我 运行 多次 API 调用,将每个 JSON 输出转换为 df 并将所有数据帧附加在一起。
当我只需要几个 API 调用时这很好,但是当我有多个客户 ID 需要拉取时效率低下,因为有时我必须 运行 5/10 分开 API 来电。
我认为循环可以帮助解决这个问题。鉴于我对 Python 的经验很少,我查看了有关循环 APIs 的其他问题,但我找不到解决方案。
下面是我使用的代码。如何进行单个 API 循环调用多个客户 ID(请记住,每次调用有大约 20 个 ID 的限制)和 returns 单个数据帧?
谢谢!
#list of customer ids
customer_id = [
"1004rca402itas8470der874",
"1004rca402itas8470der875,
"1004rca402itas8470der876",
"1004rca402itas8470der877",
"1004rca402itas8470der878",
"1004rca402itas8470der879"
]
#API call
payload = {'customer':",".join(customer_id), 'countries':'DE, 'granularity':'daily', 'start_date':'2021-01-01', 'end_date':'2022-03-31'}
response = requests.get('https://api.xxxxxxjxjx.com/t3/customers/xxxxxxxxxxxx?auth_token=xxxxxxxxxxxx', params=payload)
response.status_code
#convert to dataframe
api = response.json()
df = pd.DataFrame(api)
df['sales'] = df['domestic_sales'] + df['international_sales']
df = df[['customer_id','country','date','sales']]
df.head()
大致思路如下:
# List of dataframes
dfs = []
# List of lists of 20 customer ids each
ids = [customer_id[i:i+20] for i in range(0, len(customer_id), 20)]
# Iterate on 'ids' to call api and store new df in list called 'dfs'
for chunk in ids:
payload = {
"customer": ",".join(chunk),
"countries": "DE",
"granularity": "daily",
"start_date": "2021-01-01",
"end_date": "2022-03-31",
}
response = requests.get(
"https://api.xxxxxxjxjx.com/t3/customers/xxxxxxxxxxxx?auth_token=xxxxxxxxxxxx",
params=payload,
)
dfs.append(pd.DataFrame(response.json()))
# Concat all dataframes
df = dfs[0]
for other_df in dfs[1:]:
df = pd.concat([df, other_df])
# Additional work
df['sales'] = df['domestic_sales'] + df['international_sales']
df = df[['customer_id','country','date','sales']]