有一个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
发布评论