I've been trying to resolve this the whole day and I can't figure out a solution. Please help !! So to learn web scraping, I've been practicing on this website :
https://www.net-a-porter.com/fr/fr/Shop/Designers/Fendi
The goal is to scrape the price of EVERY PRODUCT. So, thanks to the ressources on this website and other internet users, I made this code that works perfectly :
option <- remDr$findElement(using = 'xpath', "//*/option[@value = 'view_all']")
option$clickElement()
priceNodes <- remDr$findElements(using = 'css selector', ".price")
price<-unlist(lapply(priceNodes, function(x){x$getElementText()}))
price<-gsub("€","",price)
price<-gsub(",","",price)
price <- as.numeric(price)
So with this I got the result that I want, which is a list of 204 values (price). Now I'd like to transform this entire process into a function in order to apply this function to a list of adresse (in this case to other brands). And obviously it did not work ... :
FPrice <- function(x) {
url1 <- x
remDr <- rD$client
remDr$navigate(url1)
iframe <- remDr$findElement("css", value=".view-more-less")
option <- remDr$findElement(using = 'xpath', "//*/option[@value = 'view_all']")
option$clickElement()
priceNodes <- remDr$findElements(using = 'css selector', ".price")
price<-unlist(lapply(priceNodes, function(x){x$getElementText()}))
}
When I apply it like this :
FPrice("https://www.net-a-porter.com/fr/fr/Shop/Designers/Fendi")
Error message came up and I don't get the data that I am looking for :
Selenium message:stale element reference: element is not attached to the page document
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.12.6 x86_64)
I think it is because there is a function inside of the function... Can anyone please help me the resolve the problem ? Thanks.
Ps. With rvest I made another code :
Price <- function(x) {
url1 <- x
webpage <- read_html(url1)
price_data_html <- html_nodes(webpage,".price")
price_data <- html_text(price_data_html)
price_data<-gsub("€","",price_data)
price_data<-gsub(",","",price_data)
price_data <- as.numeric(price_data)
return(price_data)
}
And it worked fine. I even applied it to a vector containing a list of adresse. However, with rvest I can not get to configure the browser so it select the option "show all". Thus I only get 60 observations while some brands propose more than 200 product, like the case of Fendi.
Thank you very much for your patience. Hope to read from you very soon !