Python的请求参数/处理API分页 [英] Python requests arguments/dealing with api pagination
问题描述
我与天使列表(AL)API玩耍,并要拉在San旧金山的所有作业。
因为我无法找到该API的积极Python包装(如果我没有取得任何进展,我想我会做我自己),我使用的请求库。
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.
美联API的结果是分页,我想不出如何超越结果的第一页。
The AL API's results are paginated, and I can't figure out how to move beyond the first page of the results.
下面是我的code:
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 ['页'] = 2
我猜测它的东西比较简单,但我似乎无法弄清楚所以任何帮助将是真棒。
I'm guessing it's something relatively simple, but I can't seem to figure it out so any help would be awesome.
感谢一如既往。
天使列表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屋!