我一直在试图解决这一整天,我找不出解决方案。请帮忙 !! 所以学习网页刮,我一直在练习本网站:Rselenium网页抓取:作为函数应用
https://www.net-a-porter.com/fr/fr/Shop/Designers/Fendi
目标是刮每一件产品的价格。 所以,感谢这个网站和其他互联网用户在ressources,我做了这个代码工作完美:
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)
本
所以我得到了我想要的结果,这是(204个值的标价)。 现在我想将整个过程转换为一个函数,以便将此函数应用于地址列表(在本例中为其他品牌)。 很显然它不工作...:
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()}))
}
当我申请这样的:
FPrice("https://www.net-a-porter.com/fr/fr/Shop/Designers/Fendi")
错误消息过来了,我不明白,我寻找的数据:
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)
我认为这是因为里面有一个函数...任何人都可以帮我解决问题吗?谢谢。
Ps。随着我做了另一个代码:
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)
}
它工作得很好。我甚至将它应用于包含地址列表的矢量。但是,在rvest中,我无法配置浏览器,因此请选择“全部显示”选项。因此,我只能得到60个观察结果,而一些品牌提出超过200个产品,就像Fendi那样。
非常感谢您的耐心等待。希望能尽快给您解读!