从API请求中指定page

系统教程 行业动态 更新时间:2024-06-14 17:03:54
从API请求中指定page_count参数(Specify page_count parameter from API request)

我正在从API下载json数据,但是我在设置参数时遇到了问题。 具体来说,我想创建一个选项,设置我想从API下载多少页面。 我的代码看起来像这样:

# Libraries import json, requests # Define API, endpoint, and page number url = 'https://api-v2.themuse.com/jobs?page=1' # Set page count parameter params = {'page_count': 10} # Request data from API and store into object resp = requests.get(url=url, params=params) # Save data as JSON data = json.loads(resp.text)

但是,'params'功能没有设置我想要下载的页数:我下载100页,而不是10页。 有什么想法吗? 如果它有帮助,JSON结构看起来像这样:

{ "page": 1, "page_count": 100, "took": 10, "timed_out": false, "total": 35210 }

I'm downloading json data from an API, but I am having trouble setting a parameter. Specifically, I'd like create the option of setting how many number of pages I'd like to download from the API. My code looks something like this:

# Libraries import json, requests # Define API, endpoint, and page number url = 'https://api-v2.themuse.com/jobs?page=1' # Set page count parameter params = {'page_count': 10} # Request data from API and store into object resp = requests.get(url=url, params=params) # Save data as JSON data = json.loads(resp.text)

However, the 'params' function doesn't set the number of pages I want to download: Instead of 10 pages, I download 100 pages. Any thoughts? If it helps, the JSON structure looks something like this:

{ "page": 1, "page_count": 100, "took": 10, "timed_out": false, "total": 35210 }

最满意答案

查看API规范 , page_count是输出参数,而不是您指定的内容。 响应数据分页,每页20个结果。

您正在请求page=1 (索引从第0页开始)并返回20个结果。 要获取前10页的所有数据,请执行以下操作:

jobs_url = 'https://api-v2.themuse.com/jobs' for i in range(11): results_data = json.loads(requests.get( url=jobs_url, params={'page': i} ).text)['results'] #do something with the data

Looking at the API specification, page_count is an output parameter and not something you specify. Response data is paginated with 20 results per page.

You are requesting page=1 (the index starts from page 0) and getting back 20 results. To get all the data for the first 10 pages, do something like this:

jobs_url = 'https://api-v2.themuse.com/jobs' for i in range(11): results_data = json.loads(requests.get( url=jobs_url, params={'page': i} ).text)['results'] #do something with the data

更多推荐

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

发布评论

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

>www.elefans.com

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