r语言 - 如何在 rvest 中跟踪具有特定 id 的元素链接?



我目前正在浏览这个网站:

https://uws-community.symplicity.com/index.php?s=student_group

因此,网站上的每个俱乐部都有一定的ID,并且还有一个"更多信息"链接。我已经找到了一种从div.grpl-grp.clearfix 类中抓取每个 id 的方法,但我想使用这些 ID 从具有该特定 id 的元素的"更多信息"链接(例如 fb 链接(中抓取数据。

这样做的语法是什么?

"更多信息"文本具有类"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"
...

最新更新