【问题标题】:How to follow link of element with a certain id in rvest?如何在 rvest 中跟踪具有特定 id 的元素的链接?
【发布时间】:2018-08-27 04:57:20
【问题描述】:
【问题讨论】:
标签:
r
web-scraping
data-science
rvest
【解决方案1】:
“更多信息”文本的类别为“grpl-moreinfo”,链接位于<a> 标记中。所以我们可以做
library(rvest)
url <- 'https://uws-community.symplicity.com/index.php?s=student_group'
page <- html_session(url)
html_nodes(page, "li.grpl-moreinfo a") %>% html_attr("href")
#[1] "?mode=form&id=5bf9ea61bc46eaeff075cf8043c27c92&tab=profile"
#[2] "?mode=form&id=17e4ea613be85fe019efcf728fb6361d&tab=profile"
#[3] "?mode=form&id=d593eb48fe26d58f616515366a1e677b&tab=profile"
...
这也可以在一个链操作中完成:
url %>%
read_html() %>%
html_nodes("li.grpl-moreinfo a") %>%
html_attr("href")
#[1] "?mode=form&id=5bf9ea61bc46eaeff075cf8043c27c92&tab=profile"
#[2] "?mode=form&id=17e4ea613be85fe019efcf728fb6361d&tab=profile"
#[3] "?mode=form&id=d593eb48fe26d58f616515366a1e677b&tab=profile"
...