迭代循环不适用于API

编程入门 行业动态 更新时间:2024-10-23 05:38:17
本文介绍了迭代循环不适用于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中的完整脚本,位于此链接 colab. research.google/drive/14KddVLTyH3LkcE-LmHm7EooTYMM7b0zB?usp=sharing

Complete script in colab at this link colab.research.google/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

本文发布于:2023-11-23 14:06:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   迭代   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!