简体   繁体   English

API 分页循环

[英]API pagination loop

I have successfully created a loop to paginate an API I am working with.我已经成功创建了一个循环来对我正在使用的 API 进行分页。 My challenge is on concatenating the dataframes once I am done with the loop so that I have one solid dataframe.我的挑战是在完成循环后连接数据帧,以便我拥有一个可靠的 dataframe。 Any help will go a long way.任何帮助都会让 go 有很长的路要走。

import requests
import json
import pandas as pd
from pandas import json_normalize

url = 'https://vendors.paddle.com/api/2.0/subscription/users'
headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'PostmanRuntime/7.29.2',
    'Accept': '*/*',
    'Accept-Encoding': 'gzip',
    'Connection': 'keep-alive'}

page = 1
data_nested = []

while data_nested is not None:
    data = [('vendor_id', xxxxx),('vendor_auth_code','4803155ec5f5a17d589b650cxxxxxxxxx'),('results_per_page',200),('page',page)]
    response = requests.post(url, headers=headers , data = data)
    data_nested = response.json()['response']
    data_flattened = pd.json_normalize(data_nested)
    df = pd.DataFrame.from_dict(data_flattened)
    print(df)
    if len(df.index)==0:
        break
    page += 1

If I'm reading this correctly you are currently just printing them out?如果我没看错,你现在只是把它们打印出来? You could do something like this if I am understanding what you want correctly.如果我正确理解你想要什么,你可以做这样的事情。 Then print it out as one big df at the end.然后在最后将其打印为一个大 df 。

 page = 1 data_nested = [] loop = [] while data_nested is not None: data = [('vendor_id', xxxxx),('vendor_auth_code','4803155ec5f5a17d589b650cxxxxxxxxx'),('results_per_page',200),('page',page)] response = requests.post(url, headers=headers, data = data) data_nested = response.json()['response'] data_flattened = pd.json_normalize(data_nested) df = pd.DataFrame.from_dict(data_flattened) loop.append(df) if len(df.index)==0: break page += 1 print(loop)

And to end up with a dataframe, I adjusted Aaron Cloud's solution to:最后得到 dataframe,我将 Aaron Cloud 的解决方案调整为:

page = 1
data_nested = []
loop = []

while data_nested is not None:
    data = [('vendor_id', xxxxx),('vendor_auth_code','4803155ec5f5a17d589b650cxxxxxxxxx'),('results_per_page',200),('page',page)]
    response = requests.post(url, headers=headers , data = data)
    data_nested = response.json()['response']
    data_flattened = pd.json_normalize(data_nested)
    df = pd.DataFrame.from_dict(data_flattened)
    loop.append(df)
    if len(df.index)==0:
        break
    page += 1   
pd.concat(loop)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM