rm(list = ls())
library(xml2)
library(rvest)
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
url <- "https://go.drugbank.com/bio_entities/BE0002180"  #
page_1 <- read_html(url) #Creates an html document from URL
table_1 <- html_table(page_1, fill = TRUE) #Parses tables into data frames
#table_1
#class(table_1)
length(table_1)
## [1] 2
dim(table_1[[1]])
## [1] 1 3
table_1[[1]]
## # A tibble: 1 x 3
##   Name           `UniProt ID` ``     
##   <chr>          <chr>        <chr>  
## 1 Cholinesterase P06276       Details
dim(table_1[[2]])
## [1] 91  6
head(table_1[[2]])
## # A tibble: 6 x 6
##   `DrugBank ID` Name               `Drug group` `Pharmacologic~` Actions Details
##   <chr>         <chr>              <chr>        <chr>            <chr>   <chr>  
## 1 DB00772       Malathion          approved, i~ yes              "inhib~ Details
## 2 DB03672       9-N-Phenylmethyla~ experimental unknown          ""      Details
## 3 DB00122       Choline            approved, n~ unknown          "produ~ Details
## 4 DB00989       Rivastigmine       approved, i~ yes              "inhib~ Details
## 5 DB00677       Isoflurophate      approved, i~ yes              "inhib~ Details
## 6 DB00941       Hexafluronium      approved     yes              "inhib~ Details
#dim(table_1[[3]])
#class(table_1[[1]])
putput_name <- "CHE"
write.csv(table_1[[1]], paste0(dir_path,Sys.Date(),"-",putput_name,"_drug.csv"),row.names = FALSE,na = "")
write.csv(table_1[[2]], paste0(dir_path,Sys.Date(),"-",putput_name,"_drug_and_targets.csv"),row.names = FALSE,na = "")
#write.csv(table_1[[3]], paste0(dir_path,Sys.Date(),"-",putput_name,"_drug_trials.csv"),row.names = FALSE,na = "")
###############################