从电子商务页面中删除产品信息

时间:2016-04-21 13:39:32

标签: r rvest rselenium

需要从电子商务页面中删除产品信息。但页面有无限滚动。目前我只能刮掉所有显示的产品而不向下滚动。下面是它的代码。

WebBrowser

1 个答案:

答案 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越多,您获得的链接就越多。