Py学习  »  Python

如何用Python从web API中收集所有结果?

Beautiful Mind • 7 年前 • 2180 次点击  

我正在使用python脚本从web api收集数据。Web API提供最多50个结果( "size":50 )但是,我需要收集所有的结果请告诉我怎么做我的初始代码如下。提前谢谢你。

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())
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/48462
文章 [ 2 ]  |  最新文章 7 年前
QHarr
Reply   •   1 楼
QHarr    7 年前

要添加到已经给出的答案中,您可以从初始json获得总结果。然后可以使用循环来增加批处理

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)
Loïc
Reply   •   2 楼
Loïc    7 年前

所有东西都在文件里:

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

进行临床试验

根据提供的筛选参数筛选所有临床试验。过滤器 params可以是模式中的任何字段,也可以是 以下参数…

大小:限制结果的数量提供的数量(默认值为10, 最大值为50)

from:从提供的起点开始结果(默认值为0)

…

所以您只需指定一个“from”值,然后将其50乘以50。