- library(rvest)
- library(dplyr)
- keyword <- c('大国崛起','资本的故事')
- keyword_num <- function(keyword){
- baidu_url <- paste0('http://www.baidu.com/s?wd=',keyword)
- web <- read_html(baidu_url,encoding="UTF-8")
- text_res <- web %>% html_nodes("div.head_nums_cont_outer.OP_LOG > div > div.nums") %>% html_text()
- res <- gsub('[^0-9]','',text_res) %>% as.integer()
- return(res)
- }
- for(i in 1:length(keyword)){
- df[i] <- keyword_num(keyword[i])
- }
- df_res <- data.frame(keyword=keyword,num=df)