Python请求参数/处理api分页 [英] Python requests arguments/dealing with api pagination

查看:37
本文介绍了Python请求参数/处理api分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Angel List (AL) API,并希望拉取旧金山的所有工作.由于我找不到 api 的活动 Python 包装器(如果我取得任何进展,我想我想自己做),我正在使用 requests 库.

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 headway, I think I'd like to make my own), I'm using the requests library.

AL API 的结果是分页的,我不知道如何移出结果的第一页.

The AL API's results are paginated, and I can't figure out how to move beyond the first page of the results.

这是我的代码:

import requests
r_sanfran = requests.get("https://api.angel.co/1/tags/1664/jobs").json()
r_sanfran.keys()
# returns [u'per_page', u'last_page', u'total', u'jobs', u'page']
r_sanfran['last_page']
#returns 16
r_sanfran['page']
# returns 1

我尝试向 requests.get 添加参数,但是没有用.我还尝试了一些非常愚蠢的事情 - 像这样更改页面"键的值对我来说神奇地进行分页.

I tried adding arguments to requests.get, but that didn't work. I also tried something really dumb - changing the value of the 'page' key like that was magically going to paginate for me.

例如.r_sanfran['page'] = 2

我猜这是相对简单的事情,但我似乎无法弄清楚,所以任何帮助都会很棒.

I'm guessing it's something relatively simple, but I can't seem to figure it out so any help would be awesome.

一如既往的感谢.

Angel List API 文档(如果有帮助).

推荐答案

读取 last_page 并对范围内的每个页面发出 get 请求:

Read last_page and make a get request for each page in the range:

import requests

r_sanfran = requests.get("https://api.angel.co/1/tags/1664/jobs").json()
num_pages = r_sanfran['last_page']

for page in range(2, num_pages + 1):
    r_sanfran = requests.get("https://api.angel.co/1/tags/1664/jobs", params={'page': page}).json()
    print r_sanfran['page']
    # TODO: extract the data

这篇关于Python请求参数/处理api分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆