使用R语言爬取DailyMed药物信息

之前介绍过如何批量爬取NCBI中基因详细信息,今天介绍爬取DailyMed指定药物的数据。

点击查看:R语言批量爬取NCBI基因注释数据

根据指定药物药物及其ID列表(由ID可以获得该药的网页链接)爬取这些药物在DailyMed中的相关信息:
DailyMed中对药物的描述如下:

我们提取每个药物的下列信息:Marketing StatusActive ingredientWARNINGSStop use and ask a doctor if

先载入所需要的包并读入上面的列表:
library(RCurl)
library(rjson)
library(stringr)
library(XML)
library(openxlsx)

rm(list=ls())
setwd("D:\\ding")

#读入xlsx文件,第一列是Drug名,第二列是DailyMed ID:
Dailymed_file <- read.xlsx("./data/OTC_2017_Ding.xlsx","Sheet2")
根据药物ID获得该药所在网址:
Dailymed_file$Dailymed_url <- paste("https://dailymed.nlm.nih.gov/dailymed/drugInfo.cfm?setid=",Dailymed_file$DailyMed_ID,sep="")

结果如下:


接下来定义提取函数:
1. 获得指定字段marketing-status的的Node内容:
getMarketing <- function(html_txt1){
  els2 = getNodeSet(html_txt1, "//*[@id=\"marketing-status\"]")
  # 获得Node的内容,并且去除空字符:
  els2_txt <- sapply(els2,xmlValue)[!(sapply(els2,xmlValue)=="")]
  if(length(els2_txt)==0){
    return(NA)
  }
  else{
    # 去除\n:
    str_c(str_replace_all(els2_txt,"(\\n )+",""),collapse = " ")
  }
}
2. 获得指定字段Active ingredient的的Node内容:
getActiveIng <- function(html_txt1){
  els1 = getNodeSet(html_txt1, "//*[@id=\"drug-information\"]/div/ul/li[a='ACTIVE INGREDIENT' or a='Active ingredient' or a='Active Ingredient' or a='OTC - ACTIVE INGREDIENT SECTION' or a='ACTIVE INGREDIENTS' or a='Active Ing' or starts-with(a,'Active') or starts-with(a,'ACTIVE')]/div")
  # 获得Node的内容,并且去除空字符:
  els1_txt <- sapply(els1,xmlValue)[!(sapply(els1,xmlValue)=="")]
  if(length(els1_txt)==0){
    return(NA)
  }
  else{
    # 去除\n:
    str_c(str_trim(str_replace_all(els1_txt,"(\\n)+","")),collapse = " ")
  }
}
3. 获得指定字段warnings的的Node内容:
getWarning <- function(html_txt1){
  els3 = getNodeSet(html_txt1, "//*[@id=\"drug-information\"]/div/ul/li[a='Warnings' or starts-with(a,'Warning') or starts-with(a,'WARNING') or starts-with(a,'warning')]/div")
  # 获得Node的内容,并且去除空字符:
  els3_txt <- sapply(els3,xmlValue)[!(sapply(els3,xmlValue)=="")]
  if(length(els3_txt)==0){
    return(NA)
  }
  else{
    # 去除\n:
    str_c(str_trim(str_replace_all(els3_txt,"(\\n )+","")),collapse = " ")
  }
}
4. 获得指定字段Stop use的的Node内容:
getStopUse <- function(html_txt1){
  els5 = getNodeSet(html_txt1, "//*[@id=\"drug-information\"]/div/ul/li[a='STOP USE' or a='stop use' or starts-with(a,'stop') or starts-with(a,'Stop') or starts-with(a,'STOP')]/div")
  # 获得Node的内容,并且去除空字符:
  els5_txt <- sapply(els5,xmlValue)[!(sapply(els5,xmlValue)=="")]
  if(length(els5_txt)==0){
    return(NA)
  }
  else{
    # 去除\n:
    str_c(str_trim(str_replace_all(els5_txt,"(\\n )+","")),collapse = " ")
  }
}
进行爬取数据:

for(i in 1:nrow(Dailymed_file)){
  # 获得网址:
  doc <- getURL(Dailymed_file[i,"Dailymed_url"])
  cat(i)
  cat("\t获取网页内容成功!")
  # 获得网页内容
  html_txt1 = htmlParse(doc, asText = TRUE)
  
  # #可以省略,保存网页内容到本地文件:
  # con <- file("./PaChong/DailyMed_local.html")
  # sink(con,append = TRUE)
  # html_txt1
  # sink()
  
  #获得Active ingredient/marketing-status/warnings/ADR:
  Dailymed_file[i,"Active_Ingredient"] <- try(getActiveIng(html_txt1),silent = TRUE)
  cat("写入Active Ingredient!")
  Dailymed_file[i,"Marketing"] <- try(getMarketing(html_txt1),silent = TRUE)
  cat("写入Market!")
  Dailymed_file[i,"Warning"] <- try(getWarning(html_txt1),silent = TRUE)
  cat("写入Warning!")
  Dailymed_file[i,"Stop_use"] <- try(getStopUse(html_txt1),silent = TRUE)
  cat(paste("写入ADR!\n"))
}
爬取结果如下:

更多原创精彩视频敬请关注生信杂谈:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容