我正在从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 dataLooking 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更多推荐
发布评论