如何使用Python中的clinicalTrials.gov API获得完整结果?

问题描述 投票:0回答:1

我正在尝试能够动态地提取健康状况以访问 clinicaltrials.gov API 并获取相关研究列表以进一步查询。 (我最终会将

search_expression
变量作为输入,现在只需进行硬编码以测试 API)。我从来没有编写过这样的代码来访问这样的 API,并且我找到了一些适合这个特定网站的示例。我有一些问题:

  1. 是否可以获得完整记录,而不仅仅是第 1 页的记录?如果是的话,怎么办?
  2. 如何合并新的 API 链接而不是旧的链接?我还没有在任何地方看到任何尝试新的例子。

我的代码如下。当我运行它时,我得到 5 行 93 列,这看起来不对。我怎样才能做到这一点?最后我想要一个简单的数据框。

import requests
import json
import pandas as pd
import datetime

end_str = '&max_rnk=1000&fmt=json'
search_expression = 'Diabetes{end_str}'
base = f'https://clinicaltrials.gov/api/query/study_fields?expr={search_expression}'

print(base)



extract_fields = [
    'NCT Number',
    'Status',
    'Conditions',
    'Interventions',
    'Sponsor',
    'Study Type',
    'Collaborators',
    'Acronym',
    'Outcome Measures',
    'Sex',
    'Age',
    'Phase',
    'Enrollment',
    'Funder type',
    'Study Design',
    'Other IDs',
    'Study Start',
    'Primary Completion',
    'Study Completion',
    'First Posted',
    'Results First Posted',
    'Last Update Posted',
    'Locations',
    'Study Documents'
]

query_url = f'{base}&fields={",".join(extract_fields)}'
print(query_url)

r = requests.get(query_url)

# Check we have a successful extract with code 200
r.status_code

## dict
j = json.loads(r.content)

## dataframe
df = pd.DataFrame(j)
df.head()
python python-3.x api
1个回答
0
投票

您显示

old API
的代码以及
new API
的字段,并添加
new API
的文档链接,但不显示
old API
的文档链接 (https://classic.clinicaltrials.gov/api/gui/ref /api_urls)所以我脑子里一片混乱。

但最后我找到了一些信息。

old API
中,您必须更改
'min_rnk'
'max_rnk'
才能获取下一页。


`旧 API 的最少工作代码:

我使用

for
循环来获取几页,但您可能需要
while True
来获取所有页面。

我还使用

[:10]
为每个页面仅显示少量结果 - 这样我就可以轻松查看它们是否不同 - 而且我不会多次获得相同的页面。

我使用文档中示例中的

fields
,因为
old API
使用不同的名称,并且我找不到您在代码中使用的名称。

我使用

get(url, params)
而不是使用 url 和所有参数创建字符串。我使用
response.json()
来获得结果。

import requests

base = 'https://clinicaltrials.gov/api/query/study_fields'   # old API

# old API fields
extract_fields = [
    'NCTId',
    'Condition',
    'BriefTitle',
]

# old API parameter
params = {
    'fields': ",".join(extract_fields), 
    'expr': 'Diabetes',
    'min_rnk': 1,
    'max_rnk': 1000,
    'fmt': 'json', 
}    

for page in range(1, 4):  # use `while True` to get all pages
    print(f'--- page: {page} ---')
    
    response = requests.get(base, params=params)
    
    #print('status_code:', response.status_code)
    #print('response.ok:', response.ok)
    
    if not response.ok:
        print('response.text:', response.text)
        break
    
    data = response.json()
    #print(data['StudyFieldsResponse'].keys())
    #     dict_keys(['APIVrs', 'DataVrs', 'Expression', 'NStudiesAvail', 'NStudiesFound', 'MinRank', 'MaxRank', 'NStudiesReturned', 'FieldList', 'StudyFields'])
    data_responses = data['StudyFieldsResponse']
    #print('APIVrs :',          data_responses['APIVrs'])
    #print('DataVrs:',          data_responses['DataVrs'])
    #print('Expression:',       data_responses['Expression'])
    #print('NStudiesAvail:',    data_responses['NStudiesAvail'])
    #print('NStudiesFound:',    data_responses['NStudiesFound'])
    #print('MinRank:',          data_responses['MinRank'])
    #print('MaxRank:',          data_responses['MaxRank'])
    #print('NStudiesReturned:', data_responses['NStudiesReturned'])
    #print('FieldList:',        data_responses['FieldList'])
    
    # I use `[:10]` to reduce results on screen
    for index, item in enumerate(data_responses['StudyFields'][:10], 1):
        print(f'{page:2},{index:4}:', item['BriefTitle'])
    
    params['min_rnk'] += 1000
    params['max_rnk'] += 1000

结果:

--- page: 1 ---
 1,   1: ['Diabetes Pueblo Program - Application and Acceptability of Culturally Appropriate Latino Education for Insulin Therapy']
 1,   2: ['Best Practice Study of Diabetes Type 2 Management in Primary Care in Switzerland']
 1,   3: ['Diabetes and Partnership: Evaluation of a Diabetes Education Module']
 1,   4: ['Diabetes and Sports: Evaluation of a Diabetes Education Module']
 1,   5: ['Diabetes and Travel: Evaluation of a Diabetes Education Module - a Randomized Controlled Trial (PRIMO_Travel)']
 1,   6: ['Diabetes and Social Issues: Evaluation of a Diabetes Education Module']
 1,   7: ['Evaluation of a Diabetes-specific Cognitive Behavioural Treatment for Subthreshold Depression']
 1,   8: ['Evaluating the Influence of Diabetes Stigma on Medication Adherence']
 1,   9: ['Diabetes to Go: Inpatient Education Implementation']
 1,  10: ['My Diabetes Care Mobile: A Usability Study']
--- page: 2 ---
 2,   1: ['Left Ventricular DYsfunction in DiAbetes']
 2,   2: ['Glucose Variability in Patients With Type 1 Diabetes With a Sucrose-added Diet']
 2,   3: ['Surgery Versus Best Medical Management for the Long Term Remission of Type 2 Diabetes and Related Diseases (REMISSION)']
 2,   4: ['Evaluation of an Intervention for Young Adults With Diabetes: Resilient, Empowered, Active Living-Telehealth (REAL-T)']
 2,   5: ['Telehealth for Diabetes Self-Management']
 2,   6: ['Comparison of Visceral Adipokines Visfatin, Vaspin and Omentin Levels in Gestational Diabetes Mellitus Pregnant Women']
 2,   7: ["Uncovering the 'ORIGINS' of Diabetes"]
 2,   8: ['Using of Ice Cream for Diagnosis of Diabetes Mellitus']
 2,   9: ['Validation Study of a Peri-intervention Diabetes Management Algorithm (DIAPI) for Endoscopy Procedures']
 2,  10: ['Vaccination Coverage in People Linving With Diabetes']
--- page: 3 ---
 3,   1: ['A Study of People With Type 2 Diabetes Who Have Diseases That Affect the Heart and Blood Vessels. The Study Will Also Look at the Treatment of a Group of These People in Hospitals in Portugal']
 3,   2: ['Randomized, Double-blind, Active-controlled, Study of Rivoglitazone in Type 2 Diabetes Mellitus']
 3,   3: ['Establishing Ambulatory Glucose Profiles for People Without Diabetes Using CGM Data']
 3,   4: ['A Pragmatic Approach to Lower Diabetes Risk After Gestational Diabetes']
 3,   5: ['Effect of GLP-1 on Insulin-dose, Risk of Hypoglycemia and Gastric Emptying Rate in Patients With Type 1 Diabetes']
 3,   6: ['The Influence of Glycemic Control and Obesity on Energy Balance and Metabolic Flexibility in Type 1 Diabetes']
 3,   7: ['Efficacy of the Digital Platform for Diabetes Care Compared to Usual Care in Patients Diagnosed With Type 2 Diabetes.']
 3,   8: ['Effect of Short-Term Beta-Cell Rest in Adolescents and Young Adults With Type 2 Diabetes Mellitus']
 3,   9: ['Weight Loss Study for People With Type 2 Diabetes']
 3,  10: ['Oral Glucose Tolerance Testing After Gestational Diabetes']
© www.soinside.com 2019 - 2024. All rights reserved.