【问题标题】:R return multiple nodes in 1 search using rvest (massive list of urls)R使用rvest(大量的url列表)在1次搜索中返回多个节点
【发布时间】:2016-09-26 15:17:14
【问题描述】:

我正在使用 rvest 抓取网站。它有效,购买效率极低,我不知道如何让它更好地工作。

in url 是超过 10.000 个 url 的列表。

number <- sapply(url, function(x)
  read_html(x) %>%
  html_nodes(".js-product-artnr") %>%
  html_text())

price_new <- sapply(url, function(x)
  read_html(x) %>%
  html_nodes(".product-page__price__new") %>%
  html_text())

price_old <- sapply(url, function(x)
  read_html(x) %>%
  html_nodes(".product-page__price__old") %>%
  html_text())

上面的问题是,rvest 访问 10.000 个 url 以获取“.js-product-artnr”中的第一个节点,然后再次访问相同的 10.000 个 url 以获取第二个节点,依此类推。最后,我预计这 10.000 个页面中需要大约 10 个不同的节点。将它们一一获取并稍后组合成一个数据框需要很长时间,必须有更好的方法。

我正在寻找类似下面的内容,以便在 1 次搜索中获取所有信息

info <- sapply(url, function(x)
  read_html(x) %>%
  html_nodes(".js-product-artnr") %>%
  html_nodes(".product-page__price__new") %>%
  html_nodes(".product-page__price__old") %>%
  html_text())

【问题讨论】:

  • 只需将第一个结果保存到变量page&lt;-read_html(x),然后进行所有提取page %&gt;% html_nodes(".js-product-artnr") %&gt;% html_text(); page %&gt;% html_nodes(".product-page__price__new") %&gt;% html_text() 等。从您的示例中,我不清楚您希望结果是什么数据结构。
  • 也许我对类似问题的回答会有所帮助:stackoverflow.com/questions/39685989/…

标签: r rvest


【解决方案1】:

这对我有用。

  func <- function(url){
  sample <- read_html(url) %>%
  scrape1 <- html_nodes(sample, ".js-product-artnr")%>%
  html_text()
  scrape2 <- html_nodes(sample, ".product-page__price__new") %>%
  html_text()
  scrape3 <- html_nodes(sample,".product-page__price__old") %>%
  html_text()
  df <- cbind(scrape1, scrape2, scrape3)
  final_df <- as.data.frame(df)
return(final_df)
}

数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 2019-09-20
    相关资源
    最近更新 更多