【问题标题】:How to follow link of element with a certain id in rvest?如何在 rvest 中跟踪具有特定 id 的元素的链接?
【发布时间】:2018-08-27 04:57:20
【问题描述】:

我正在浏览这个网站:

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

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

这样做的语法是什么?

【问题讨论】:

    标签: 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"
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 2022-01-04
      • 1970-01-01
      • 2018-01-06
      相关资源
      最近更新 更多