需要从电子商务页面中删除产品信息。但页面有无限滚动。目前我只能刮掉所有显示的产品而不向下滚动。下面是它的代码。
WebBrowser
答案 0 :(得分:2)
好吧,如果滚动真的是无限的,那么就不可能获得所有的链接......如果你想要满足有限的数字,你可以在这里有效地使用RSelenium
。
library(RSelenium)
#start RSelenium
checkForServer()
startServer()
remDr <- remoteDriver()
remDr$open()
# load your page
remDr$navigate("http://www.jabong.com/kids/clothing/girls-clothing/kids-tops-t-shirts/?source=topnav_kids")
# scroll down 5 times, allowing 3 second for the page to load everytime
for(i in 1:5){
remDr$executeScript(paste("scroll(0,",i*10000,");"))
Sys.sleep(3)
}
# get the page html
page_source<-remDr$getPageSource()
# get the URL's that you are looking for
pp <- xml2::read_html(page_source[[1]]) %>%
rvest::html_nodes("a") %>%
rvest::html_attr("data-original-href") %>%
{.[!is.na(.)]}
结果是312个链接(在我的浏览器中)。向下滚动RSelenium越多,您获得的链接就越多。