迭代循环不适用于API [英] The iteration loop is not working properly for API

查看:71
本文介绍了迭代循环不适用于API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个API每页只能产生一百个结果.我试图进行一个while循环,以便它遍历所有页面并从所有页面获取结果,但是它无法正常工作.

There is an API that only produces one hundred results per page. I am trying to make a while loop so that it goes through all pages and takes results from all pages, but it does not work properly.

此脚本遍历页面:

params = dict(
    order_by='salary_desc',
    text=keyword,
    area=area,
    period=30, # days
    per_page=100,
    page = 0,
    no_magic='false',  # disable magic
    search_field='name'  # available: name, description, company_name
)
pages = []
while True:
  params["page"] += 1
  response = requests.get(BASE_URL + '/vacancies', headers={'User-Agent': generate_user_agent()}, params=params,)
  items = response.json()['items']
  if not items:
    break
  pages.append(items) # Do it for each page
response

启动时:

params

{'area': 1,
 'no_magic': 'false',
 'order_by': 'salary_desc',
 'page': 5,
 'per_page': 100,
 'period': 30,
 'search_field': 'name',
 'text': '"python"'}

他看到五页.

执行后查看变量:

len(pages)
4

他只看到四页.

如果我理解正确,他看不到零页面(api中的页面从零开始).

If I understood correctly, he does not see the zero page (pages in the api start at zero).

请告诉我如何解决此错误?

Please tell me how you can fix this error?

在colab中的完整脚本,位于此链接 https://colab. research.google.com/drive/14KddVLTyH3LkcE-LmHm7EooTYMM7b0zB?usp=sharing

Complete script in colab at this link https://colab.research.google.com/drive/14KddVLTyH3LkcE-LmHm7EooTYMM7b0zB?usp=sharing

推荐答案

您在获取响应之前先递增页面.像这样重新排序.

You are incrementing the page prior to grabbing the response. Just reorder like so.

while True:
  response = requests.get(BASE_URL + '/vacancies', headers={'User-Agent': generate_user_agent()}, params=params,)
  items = response.json()['items']
  if not items:
    break
  pages.append(items) # Do it for each page
  params["page"] += 1

这篇关于迭代循环不适用于API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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