【问题标题】:Python / BeautifulSoup - Extracting XML data from clinicaltrials.gov, only able to extract studies that don't have missing dataPython / BeautifulSoup - 从 Clinicaltrials.gov 中提取 XML 数据,只能提取没有缺失数据的研究
【发布时间】:2021-12-29 21:34:19
【问题描述】:

我正在使用临床试验.gov 的 API 将临床试验数据列表获取到 XML 文件中,然后我正在解析数据以最终导出到 Excel 数据集。

在我的代码中提供的 URL 中,有 9 个结果,但是我的代码仅提取 5/9 的数据。我意识到这是因为对于其中一个字段 (detaileddescription),只有一些试验有这些数据。当我删除 detaileddescription 并仅使用其他两个字段(nctid 和 briefdescription)时,我可以获得 9/9。除了为 detaileddescription 创建一个单独的数据框并合并之外,我还能在这里做什么?

底线:我正在从包含 9 个临床试验的 XML 文件中提取 3 个字段:nctidbriefsummary详细描述,但我的输出只提取了 5/9 的临床试验。如果不从我的输出中取出 detaileddescription 字段,我的输出如何获得全部 9/9?

import requests
from bs4 import BeautifulSoup
import pandas as pd

out = []
url = 'https://clinicaltrials.gov/api/query/full_studies?expr=diabetes+telehealth+peer+support&+AREA%5BStartDate%5D+EXPAND%5BTerm%5D+RANGE%5B01%2F01%2F2020%2C+09%2F01%2F2020%5D&min_rnk=1&max_rnk=50&fmt=xml'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
nctids = soup.find_all("field", {"name" : "NCTId"})
briefsummaries = soup.find_all("field", {"name" : "BriefSummary"}) if soup.find_all("field", {"name" : "BriefSummary"}) is not None else 'nothing'
detaileddescriptions = soup.find_all("field", {"name" : "DetailedDescription"}) if soup.find_all("field", {"name" : "DetailedDescription"}) is not None else 'nothing'

for nctid, briefsummary, detaileddescription in zip(nctids, briefsummaries, detaileddescriptions):
    
    data = {'nctid': nctid, 'briefsummary': briefsummary, 'detaileddescription': detaileddescription}
    out.append(data)
df = pd.DataFrame(out)

df.to_excel('clinicaltrialstresults.xlsx')

【问题讨论】:

  • nctid 和briefsummary 中有多少项?我的猜测是,您循环的 zip 在最短的压缩向量(详细描述)的末尾停止。
  • nctid 和 Briefsummary 每个临床试验只有一个条目,试验的 XML 文件中有 9/9 有这些数据。只有 5/9 有详细的描述。如果这是 SQL,我只需将它们分成两个表并进行左连接或其他操作,但我假设我可以在这里使用 zip 做一些事情(即使我可以放置某种虚拟响应).. .
  • 第二,有一次字典没有保证顺序(尽管我认为这可能已经改变了?),所以你可能没有通过单独创建字典来将正确的 id/summary/descriptions 相互关联。你能找到上一级标签,并创建具有 3 个属性的单个对象吗?
  • 如果它给你一个 xml 文件,也许直接将它读入 pandas 数据帧 (df.read_xml(response))?
  • 呃,好点子。我没有想到这些被分离的可能性。

标签: python pandas xml beautifulsoup text-parsing


【解决方案1】:

您可以尝试通过对代码稍作更改来遍历研究列表

import requests
from bs4 import BeautifulSoup
import pandas as pd


out = []
url = 'https://clinicaltrials.gov/api/query/full_studies?expr=diabetes+telehealth+peer+support&+AREA%5BStartDate%5D+EXPAND%5BTerm%5D+RANGE%5B01%2F01%2F2020%2C+09%2F01%2F2020%5D&min_rnk=1&max_rnk=50&fmt=xml'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
study_list = soup.find_all("fullstudy")

for study in study_list:
    nctid = study.find("field", {"name" : "NCTId"})
    briefsummary = study.find("field", {"name" : "BriefSummary"}) if study.find("field", {"name" : "BriefSummary"}) is not None else 'nothing'
    detaileddescription = study.find("field", {"name" : "DetailedDescription"}) if study.find("field", {"name" : "DetailedDescription"}) is not None else 'nothing'
    data = {'nctid': nctid, 'briefsummary': briefsummary, 'detaileddescription': detaileddescription}
    out.append(data)

df = pd.DataFrame(out)
df.to_excel('clinicaltrialstresults.xlsx', index=False)

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 2021-04-28
    • 2021-03-18
    • 2021-03-20
    • 2019-03-24
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多