当我在clinicaltrials.gov上输入搜索词时,我想把这个数据表刮一下.具体来说,我想把你在这一页上看到的表格刮一下:https://clinicaltrials.gov/ct2/results?term=nivolumab+AND+Overall+Survival.屏幕截图如下:

enter image description here

我try 过这段代码,但我认为我没有找到正确的css Select 器:

# create custom url
ctgov_url <- "https://clinicaltrials.gov/ct2/results?term=nivolumab+AND+Overall+Survival"
# read HTML page
ct_page <- rvest::read_html(ctgov_url)

# extract related terms
ct_page %>%
  # find elements that match a css selector
  rvest::html_element("t") %>%
  # retrieve text from element (html_text() is much faster than html_text2())
  rvest::html_table()

推荐答案

你根本不需要rvest.该页面提供了一个下载按钮来获取搜索项目的csv.这有一个基本的url编码GET语法,允许您创建一个简单的小API:

get_clin_trials_data <- function(terms, n = 1000) {
  
  terms<- URLencode(paste(terms, collapse = " AND "))

  df <- read.csv(paste0(
    "https://clinicaltrials.gov/ct2/results/download_fields",
    "?down_count=", n, "&down_flds=shown&down_fmt=csv",
    "&term=", terms, "&flds=a&flds=b&flds=y"))

  dplyr::as_tibble(df)
}

这允许您传入搜索词向量和要返回的最大结果数.不需要像web抓取那样进行复杂的解析.

get_clin_trials_data(c("nivolumab", "Overall Survival"), n = 10)
#> # A tibble: 10 x 8
#>     Rank Title     Status Study.Results Conditions Interventions Locations URL  
#>    <int> <chr>     <chr>  <chr>         <chr>      <chr>         <chr>     <chr>
#>  1     1 A Study ~ Compl~ No Results A~ Hepatocel~ ""            "Bristol~ http~
#>  2     2 Nivoluma~ Activ~ No Results A~ Glioblast~ "Drug: Nivol~ "Duke Un~ http~
#>  3     3 Nivoluma~ Unkno~ No Results A~ Melanoma   "Biological:~ "CHU d'A~ http~
#>  4     4 Study of~ Compl~ Has Results   Advanced ~ "Biological:~ "Highlan~ http~
#>  5     5 A Study ~ Unkno~ No Results A~ Brain Met~ "Drug: Fotem~ "Medical~ http~
#>  6     6 Trial of~ Compl~ Has Results   Squamous ~ "Drug: Nivol~ "Stanfor~ http~
#>  7     7 Nivoluma~ Compl~ No Results A~ MGMT-unme~ "Drug: Nivol~ "New Yor~ http~
#>  8     8 Study of~ Compl~ Has Results   Squamous ~ "Biological:~ "Mayo Cl~ http~
#>  9     9 Study of~ Compl~ Has Results   Non-Squam~ "Biological:~ "Mayo Cl~ http~
#> 10    10 An Open-~ Unkno~ No Results A~ Squamous-~ "Drug: Nivol~ "IRCCS -~ http~

reprex package(v2.0.1)于2022年6月21日创建

R相关问答推荐

如果索引重复,聚合xts核心数据

在边界外添加注释或标题

如何使用shinyChatR包配置聊天机器人

在垂直轴中包含多个ggplot2图中的平均值

R中的子集文件—读取文件名索引为4位数字序列,例如0001到4000,而不是1到4000)

将向量组合到一个数据集中,并相应地命名行

自动变更列表

打印XTS对象

使用范围和单个数字将数字与字符串进行比较

在GG图中绘制射线的自动程序

R-按最接近午夜的时间进行筛选

如何在R中通过多个变量创建交叉表?

按时间顺序对不同事件进行分组

在R中创建连续的期间

将标识符赋给事件序列,避免错误观察

调换行/列并将第一行(原始数据帧的第一列)提升为标题的Tidyr类似功能?

将Geojson保存为R中的shapefile

如何准确地指出Read_delim所面临的问题?

如何将数据框压缩为更宽,同时将行输入保持为行输入,而不是R中的列名?

为各个小节生成单独的选项卡