API capture all paginated data? (python)

前端 未结 1 1727
南旧
南旧 2021-02-06 14:18

I\'m using the requests package to hit an API (greenhouse.io). The API is paginated so I need to loop through the pages to get all the data I want. Using something like:

1条回答
  •  礼貌的吻别
    2021-02-06 15:03

    The python requests library (http://docs.python-requests.org/en/latest/) can help here. The basic steps will be (1) all the request and grab the links from the header (you'll use this to get that last page info), and then (2) loop through the results until you're at that last page.

    import requests
    
    results = []
    
    response = requests.get('https://harvest.greenhouse.io/v1/applications', auth=('APIKEY',''))
    raw = response.json()  
    
    for i in raw:  
        results.append(i) 
    
    while response.links['next'] != response.links['last']:  
        r = requests.get(r.links['next'], auth=('APIKEY', '')  
        raw = r.json()  
        for i in raw:  
            results.append(i)
    

    0 讨论(0)
提交回复
热议问题