简体   繁体   English

如何使用Python从Web API收集所有结果?

[英]How to collect all results from a web API in Python?

I am collecting data from a web API by using a Python script. 我正在使用Python脚本从Web API收集数据。 The web API provides maximum 50 results ( "size":50 ). Web API最多提供50个结果( "size":50 )。 However, I need to collect all the results. 但是,我需要收集所有结果。 Please let me know how can I do it. 请让我知道我该怎么做。 My initial code is available below. 我的初始代码如下。 Thank you in advance. 先感谢您。

def getData():
    headers = {
    'Content-type': 'application/json',
    }

    data = '{"size":50,"sites.recruitment_status":"ACTIVE", "sites.org_state_or_province":"VA"}'
    response = requests.post('https://clinicaltrialsapi.cancer.gov/v1/clinical-trials', headers=headers, data=data)

    print(response.json())

Everything is in the doc : 一切都在文档中:

https://clinicaltrialsapi.cancer.gov/#!/Clinical45trials/searchTrialsByGet https://clinicaltrialsapi.cancer.gov/#!/Clinical45trials/searchTrialsByGet

GET clinical-trials GET临床试验

Filters all clinical trials based upon supplied filter params. 根据提供的过滤器参数过滤所有临床试验。 Filter params may be any of the fields in the schema as well as any of the following params... 筛选器参数可以是架构中的任何字段,也可以是以下任何参数...

size: limit the amount of results a supplied amount (default is 10, max is 50) size:限制所提供结果的数量(默认为10,最大为50)

from: start the results from a supplied starting point (default is 0) from:从提供的起点开始结果(默认为0)

... ...

So you just have to specify a "from" value, and increment it 50 by 50. 因此,您只需要指定一个“ from”值,然后将其50增加50。

To add to the answer already given you can get then total results from the initial json. 要添加到已经给出的答案中,您可以从初始json中获得总结果。 You can then use a loop to increment for batches 然后,您可以使用循环来增加批次

import requests
import json

url = "https://clinicaltrialsapi.cancer.gov/v1/clinical-trials"
r = requests.get(url).json()
num_results = int(r['total'])
results_per_request = 50
total = 0
while total < num_results:
    total+=results_per_request
    print(total)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM