【问题标题】:How to add several class types inside a function while doing web scraping in R在 R 中进行网页抓取时如何在函数中添加多个类类型
【发布时间】:2021-05-10 12:16:50
【问题描述】:

我正在尝试使用 R 函数从网站收集多个值,我拥有的代码如下:

automatic = function(ETF) {
  ETF = read_html(paste0('https://www.justetf.com/de-en/etf-profile.html?query=',ETF,'&groupField=index&from=search&isin=',ETF,'#overview'))
  ETF = ETF %>%
        general_info = ETF%>%html_nodes(".val") %>% html_text()
        name_ETF = ETF%>%html_nodes(".h1") %>% html_text()
  return(ETF)

}

如果我只是使用“.val”类,该函数可以工作,我可以使用 sapply 检索数据并将其保存到 data.frame,但是,如何在使用这两个类(.val 和 .h1 )?

提前谢谢你!

编辑:

这是我一直用来从 .val 获取信息的代码,但我现在想添加 .h1 类

automatic = function(ETF) {
  ETF = read_html(paste0('https://www.justetf.com/de-en/etf-profile.html?query=',ETF,'&groupField=index&from=search&isin=',ETF,'#overview'))
  ETF = ETF %>%
        html_nodes(".val") %>% html_text()
  return(ETF)

}

【问题讨论】:

    标签: r web-scraping


    【解决方案1】:

    固定

    > automatic = function(ETF) {
      ETF = read_html(paste0('https://www.justetf.com/de-en/etf-profile.html?query=',ETF,'&groupField=index&from=search&isin=',ETF,'#overview'))
      ETF = ETF %>%
            html_nodes(".h1, .val") %>% html_text()
      return(ETF)
    

    }

    只需简单地将类传递为 html_nodes(".h1, .val, .etc")

    【讨论】:

      猜你喜欢
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 2020-08-25
      • 1970-01-01
      相关资源
      最近更新 更多