library(XML)
doc <- htmlParse("http://www.priceminister.com/nav/Informatique_Ordinateur-portable/f1/Apple/kw/ipad")
# titre des annonces
titre <- xpathSApply(doc, "//div[@class='announce_blk ']/h3/a", xmlValue)
# lien vers les annonces
lien <- xpathSApply(doc, "//div[@class='announce_blk ']/h3/a", xmlGetAttr, name = "href")
lien <- gsub("(/offer/buy/\\d+/).+$","\\1sort0/all", lien, perl = T)
lien <- paste("http://www.priceminister.com", lien, sep="")
# On crée un data.frame où chaque ligne représente une version différente de l'ipad
produit <- data.frame(titre)
#on rajoute à la main des informations sur chaque produit
produit <- edit(produit)
names(produit) <- c("titre","version","ggg","go","couleur")
getAnnonces <- function(i) {
url <- lien[i]
doc <- htmlParse(url)
annonce <- getNodeSet(doc, "//div[contains(@class,'announce_blk')]")
N <- length(annonce)
df <- data.frame(produit=rep(i, N),
prix=NA,
note=NA,
ventes=NA,
etat=NA)
df$prix <- xpathSApply(doc, "//li[@class='price']/span", xmlValue)
df$etat <- xpathSApply(doc, "//li[@class='advtype']/span", xmlValue)
df$id <- xpathSApply(doc, "//li[@class='links']/a[@class='see_details']",xmlGetAttr, name="href")
for(i in 1:N) {
try(df$note[i] <- xpathSApply(annonce[[i]], ".//i[@class='seller_mark']/b", xmlValue), silent =TRUE)
try(df$ventes[i] <- xpathSApply(annonce[[i]], ".//i[@class='sales']/b", xmlValue), silent =TRUE)
}
return(df)
}
webscraper <- function(lien) {
data <- NULL
error <- NULL
for (i in 1:length(lien)) {
tmp <- NULL
try(tmp <- getAnnonces(i))
if(is.null(tmp)) {
error <- c(error, i)
} else {
data <- rbind(data, tmp)
}
}
return(list(data=data, error=error))
}
ipad <- webscraper(lien)
ipad$error
ipad <- ipad$data
ipad$etat <- gsub("\n", "", ipad$etat)
ipad$etat <- as.factor(ipad$etat)
ipad$ventes <- as.numeric(ipad$ventes)
ipad$ventes[is.na(ipad$ventes)] <- 0
ipad$note <- gsub("/5", "", ipad$note)
ipad$note <- gsub(",", ".", ipad$note)
ipad$note <- as.numeric(ipad$note)
ipad$prix <- gsub(" €", "", ipad$prix)
ipad$prix <- gsub(",", ".", ipad$prix)
ipad$prix <- gsub(" ", "", ipad$prix)
ipad$prix <- as.numeric(ipad$prix)
ipad$id <- gsub("^.+'(\\d+)')$","\\1",ipad$id)
ipad$id <- as.numeric(ipad$id)
for(v in c("version","ggg","go","couleur")) {
ipad[ ,v] <- produit[ipad$produit ,v]
}
ipad$version <- as.factor(ipad$version)
ipad$couleur <- as.factor(ipad$couleur)