【发布时间】: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<-read_html(x),然后进行所有提取page %>% html_nodes(".js-product-artnr") %>% html_text(); page %>% html_nodes(".product-page__price__new") %>% html_text()等。从您的示例中,我不清楚您希望结果是什么数据结构。 -
也许我对类似问题的回答会有所帮助:stackoverflow.com/questions/39685989/…