python的franme_Python的请求参数/处理API分页

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.

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

Here is my 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

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.

eg. 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.

Thanks as always.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值