Python requests arguments/dealing with api pagination

后端 未结 4 2385
时光取名叫无心
时光取名叫无心 2020-12-08 10:10

I\'m playing around with the Angel List (AL) API and want to pull all jobs in San San Francisco. Since I couldn\'t find an active Python wrapper for the api (if I make any h

4条回答
  •  温柔的废话
    2020-12-08 10:50

    Improving on @alecxe's answer: if you use a Python Generator and a requests HTTP session you can improve the performance and resource usage if you are querying lots of pages or very large pages.

    import requests
    
    session = requests.Session()
    
    def get_jobs():
        url = "https://api.angel.co/1/tags/1664/jobs" 
        first_page = session.get(url).json()
        yield first_page
        num_pages = first_page['last_page']
    
        for page in range(2, num_pages + 1):
            next_page = session.get(url, params={'page': page}).json()
            yield next_page
    
    for page in get_jobs():
        # TODO: process the page
    

提交回复
热议问题