lan*_*dge 10 r web-scraping rvest
这篇文章http://www.ajnr.org/content/30/7/1402.full包含四个指向html-tables的链接,我想用rvest来搜索.
借助css选择器:
"#T1 a"
Run Code Online (Sandbox Code Playgroud)
可以像这样到达第一个表:
library("rvest")
html_session("http://www.ajnr.org/content/30/7/1402.full") %>%
follow_link(css="#T1 a") %>%
html_table() %>%
View()
Run Code Online (Sandbox Code Playgroud)
css选择器:
".table-inline li:nth-child(1) a"
Run Code Online (Sandbox Code Playgroud)
可以选择包含链接到四个表的标签的所有四个html节点:
library("rvest")
html("http://www.ajnr.org/content/30/7/1402.full") %>%
html_nodes(css=".table-inline li:nth-child(1) a")
Run Code Online (Sandbox Code Playgroud)
如何循环遍历此列表并一次性检索所有四个表?什么是最好的方法?
had*_*ley 17
这是一种方法:
library(rvest)
url <- "http://www.ajnr.org/content/30/7/1402.full"
page <- read_html(url)
# First find all the urls
table_urls <- page %>%
html_nodes(".table-inline li:nth-child(1) a") %>%
html_attr("href") %>%
xml2::url_absolute(url)
# Then loop over the urls, downloading & extracting the table
lapply(table_urls, . %>% read_html() %>% html_table())
Run Code Online (Sandbox Code Playgroud)