加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

在R和rvest中删除多个链接的HTML表

发布时间:2020-12-14 18:59:05 所属栏目:资源 来源:网络整理
导读:本文 http://www.ajnr.org/content/30/7/1402.full包含四个链接到html表,我想用rvest刮. 在css选择器的帮助下: "#T1 a" 有可能到这样的第一个表: library("rvest")html_session("http://www.ajnr.org/content/30/7/1402.full") %%follow_link(css="#T1 a")
本文 http://www.ajnr.org/content/30/7/1402.full包含四个链接到html表,我想用rvest刮.

在css选择器的帮助下:

"#T1 a"

有可能到这样的第一个表:

library("rvest")
html_session("http://www.ajnr.org/content/30/7/1402.full") %>%
follow_link(css="#T1 a") %>%
html_table() %>%
View()

css选择器:

".table-inline li:nth-child(1) a"

使得可以选择包含链接到四个表的标签的所有四个html节点:

library("rvest")
html("http://www.ajnr.org/content/30/7/1402.full") %>%
html_nodes(css=".table-inline li:nth-child(1) a")

如何循环遍历此列表并一次检索所有四个表?最好的方法是什么?

解决方法

这是一种方法:
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())

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读