Package
library(tidyverse)
library(rvest)
library(glue)
Function
### Function to get number of result by URL
get_number_by_URL <- function(URL){
nbres <- read_html(URL) %>%
html_nodes(".gs_ab_mdw") %>%
html_text() %>% .[2] %>%
str_remove(",") %>%
str_extract("\\d+") %>%
as.numeric()
return(nbres)
}
### Function to get result for one species
get_res_one_sp <- function(sp_name){
print(sp_name)
sp_name2 <- sp_name %>% str_replace_all(pattern = " ", "+")
### search in titre
input <- glue("allintitle:+%22{sp_name2}%22")
URL <- glue("https://scholar.google.com/scholar?as_vis=1&q={input}&hl=en")
nbres_intitre <- get_number_by_URL(URL)
### search anywhere in artice
URL <- glue("https://scholar.google.com/scholar?as_q=&as_epq={sp_name2}&hl=en&as_vis=1")
nbres_anywhere <- get_number_by_URL(URL)
### output as df
dfres <- data.frame(sp_name = sp_name, nb_intitre = nbres_intitre, nb_anywhere = nbres_anywhere)
##write.csv(dfres,"dfres.csv")
return(dfres)
}
Run
For one species
get_res_one_sp("Cecropia sciadophylla")
## [1] "Cecropia sciadophylla"
## sp_name nb_intitre nb_anywhere
## 1 Cecropia sciadophylla 22 1130
For multi species
listsp <- c("Cecropia sciadophylla", "Cecropia obtusa", "Cecropia palmata")
do.call("rbind", lapply(listsp, get_res_one_sp))
## [1] "Cecropia sciadophylla"
## [1] "Cecropia obtusa"
## [1] "Cecropia palmata"
## sp_name nb_intitre nb_anywhere
## 1 Cecropia sciadophylla 22 1130
## 2 Cecropia obtusa 13 624
## 3 Cecropia palmata 8 610