1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
| library(XML)
J <- read.csv2("Z:\\Pomper des pages web\\LFP\\Saison_2011-2012\\Joueurs.txt")
M <- matrix(nrow=length(J[,1]),ncol=49,
dimnames=list(NULL,
c("Code joueur",
"Nationalité 1",
"Nationalité 2",
"Date de naissance",
"Taille",
"Poids",
"Poste",
"Buts 0-15",
"Buts 16-30",
"Buts 31-45",
"Buts AR1",
"Buts MT1",
"Buts 46-60",
"Buts 61-75",
"Buts 76-90",
"Buts AR2",
"Buts MT2",
"Buts Action de jeu",
"Buts Action de jeu - Déviation/Remise",
"Buts Centre",
"Buts Centre - Déviation/Remise",
"Buts Coup Franc - Direct",
"Buts Coup Franc - Indirect",
"Buts Coup Franc - Coup de pied arrêté",
"Buts Coup Franc - Coup de pied arrêté/Remise/Déviation",
"Buts Corner",
"Buts Corner - Direct",
"Buts Corner - Déviation/Remise",
"Buts Touche",
"Buts Touche - Déviation/Remise",
"Buts Penalty",
"Buts Coup d'envoi",
"Buts Pied droit",
"Buts Pied gauche",
"Buts Talon droit",
"Buts Talon gauche",
"Buts Poitrine",
"Buts Tête",
"Buts Autres",
"Buts Six mètres",
"Buts Surface",
"Buts En dehors de la surface",
"Buts Moitié de terrain adverse",
"Buts Nombre de clubs",
"Buts / Temps de jeu",
"Passes décisives",
"Saison",
"Code saison",
"Compétition")))
M[,47] <- "2011/2012"
M[,48] <- "80"
M[,49] <- "Ligue 1"
for(i in 1:length(M[,1]))
{
url <- as.character(J[i,4])
url <- sapply(strsplit(url,"http://www.lfp.fr/joueur/joueur/"),function(x)x[2])
a <- paste("http://www.lfp.fr/joueur/",url,sep="")
parse <- htmlParse(a)
############### Id du joueur ################
id <- sapply(strsplit(xpathSApply(parse,
"//input[id('joueur_hidden')]",
xmlGetAttr,"value")[1]," +"),function(x)x[1])
M[i,1] <- id
#############################################
############### Infos joueurs ###############
infos <- xpathSApply(parse,
"//div[@class='box']",
xmlValue)
Encoding(infos) <- "UTF-8"
I <- strsplit(sapply(strsplit(infos[2],"Club actuel"),function(x)x[1]),":")
M[i,2] <- NA
M[i,3] <- NA
M[i,4] <- NA
M[i,5] <- NA
M[i,6] <- NA
M[i,7] <- NA
M[i,45] <- 0
M[i,46] <- 0
But <- 0
for(t in 1:(length(I[[1]])-1))
{
if(length(grep("Nationalité",I[[1]][t]))==1)
{
N <- xpathSApply(parse,
"//div[@class='infos_joueur']/dl/dd/img",
xmlGetAttr,"alt")
if(length(N)==1)
{
M[i,2] <- N
M[i,3] <- NA
}
if(length(N)==2)
{
M[i,2] <- strsplit(N," ")[[1]]
M[i,3] <- strsplit(N," ")[[2]]
}
}
if(length(grep("Né le",I[[1]][t]))==1)
{
naissance <- gsub("^ +","",gsub(" +$","",gsub("\\W"," ",sapply(strsplit(I[[1]][t+1]," {3,}"),function(x)x[1]))))
M[i,4] <- naissance
}
if(length(grep("Taille",I[[1]][t]))==1)
{
taille <- gsub("^ +","",gsub(" +$","",gsub("\\D","",sapply(strsplit(I[[1]][t+1]," {3,}"),function(x)x[1]))))
M[i,5] <- taille
}
if(length(grep("Poids",I[[1]][t]))==1)
{
poids <- gsub("^ +","",gsub(" +$","",gsub("\\D","",sapply(strsplit(I[[1]][t+1]," {3,}"),function(x)x[1]))))
M[i,6] <- poids
}
if(length(grep("Poste",I[[1]][t]))==1)
{
poste <- gsub("^ +","",gsub(" +$","",gsub("\\W"," ",sapply(strsplit(I[[1]][t+1]," {3,}"),function(x)x[1]))))
M[i,7] <- poste
}
############### Temps de jeu ################
if(length(grep("Temps de jeu",I[[1]][t]))==1)
{
Temps <- as.numeric(gsub("^ +","",gsub(" +$","",gsub("\\D"," ",sapply(strsplit(I[[1]][t+1],"min"),function(x)x[1])))))
}
#############################################
############### Buts joueurs ################
if(length(grep("Buts marqués",I[[1]][t]))==1)
{
Z <- as.numeric(gsub("^ +","",gsub(" +$","",gsub("\\D"," ",sapply(strsplit(I[[1]][t+1],"\n\t\t\t\t\n\t"),function(x)x[1])))))
if(Z>0)
{
But <- Z
M[i,45] <- Temps/Z #### Buts/Minutes #####
}
}
#############################################
############### Passes joueurs ##############
if(length(grep("Passes décisives",I[[1]][t]))==1)
{
Y <- gsub("^ +","",gsub(" +$","",gsub("\\D"," ",sapply(strsplit(I[[1]][t+1],"\n\t\t\t\t\n\t"),function(x)x[1]))))
M[i,46] <- Y
}
#############################################
}
#############################################
############### Buts joueurs ################
for(s in 8:44){M[i,s] <- 0}
if(But!=0)
{
url2 <- paste("http://www.lfp.fr/joueur/showButs?jou=",id,"&sai=80&com=D1",sep="")
if(typeof(try(parse2 <- htmlParse(url2,encoding = "UTF-8"),silent=TRUE))=="externalptr")
{
T <- xpathSApply(parse2,"//body",xmlValue)
Encoding(T) <- "UTF-8"
if(T!="Pas de données disponibles.")
{
### Par période ###
P <- xpathSApply(parse2,
"//td[@class='chiffreGras']",
xmlValue)
M[i,8] <- P[1]
M[i,9] <- P[2]
M[i,10] <- P[3]
M[i,11] <- P[4]
M[i,12] <- gsub("\\D","",P[5])
M[i,13] <- P[6]
M[i,14] <- P[7]
M[i,15] <- P[8]
M[i,16] <- P[9]
M[i,17] <- gsub("\\D","",P[10])
### Par type d'action ###
TAit <- xpathSApply(parse2,
"//td[@class='intituleType']",
xmlValue)
##Encoding(TAit) <- "UTF-8"
TAnb <- xpathSApply(parse2,
"//td[@class='nbButs']",
xmlValue)
for(k in 1:length(TAit))
{
if((length(grep("Action de jeu",TAit[k]))==1)&&(length(grep("Action de jeu - Déviation/Remise",TAit[k]))==0))
{
M[i,18] <- gsub("\\D","",TAnb[k])
}
if((length(grep("Action de jeu",TAit[k]))==1)&&(length(grep("Action de jeu - Déviation/Remise",TAit[k]))==1))
{
M[i,19] <- gsub("\\D","",TAnb[k])
}
if((length(grep("Centre",TAit[k]))==1)&&(length(grep("Centre - Déviation/Remise",TAit[k]))==0))
{
M[i,20] <- gsub("\\D","",TAnb[k])
}
if((length(grep("Centre",TAit[k]))==1)&&(length(grep("Centre - Déviation/Remise",TAit[k]))==1))
{
M[i,21] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Coup Franc - Direct",TAit[k]))==1)
{
M[i,22] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Coup Franc - Indirect",TAit[k]))==1)
{
M[i,23] <- gsub("\\D","",TAnb[k])
}
if((length(grep("Coup Franc - Coup de pied arrêté",TAit[k]))==1)&&(length(grep("Coup Franc - Coup de pied arrêté/Remise/Déviation",TAit[k]))==0))
{
M[i,24] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Coup Franc - Coup de pied arrêté/Remise/Déviation",TAit[k]))==1)
{
M[i,25] <- gsub("\\D","",TAnb[k])
}
if((length(grep("Corner",TAit[k]))==1)&&(length(grep("Corner - Déviation/Remise",TAit[k]))==0)&&(length(grep("Corner - Direct",TAit[k]))==0))
{
M[i,26] <- gsub("\\D","",TAnb[k])
}
if((length(grep("Corner",TAit[k]))==1)&&(length(grep("Corner - Direct",TAit[k]))==1))
{
M[i,27] <- gsub("\\D","",TAnb[k])
}
if((length(grep("Corner",TAit[k]))==1)&&(length(grep("Corner - Déviation/Remise",TAit[k]))==1))
{
M[i,28] <- gsub("\\D","",TAnb[k])
}
if((length(grep("Touche",TAit[k]))==1)&&(length(grep("Touche - Déviation/Remise",TAit[k]))==0))
{
M[i,29] <- gsub("\\D","",TAnb[k])
}
if((length(grep("Touche",TAit[k]))==1)&&(length(grep("Touche - Déviation/Remise",TAit[k]))==1))
{
M[i,30] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Penalty",TAit[k]))==1)
{
M[i,31] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Coup d'envoi",TAit[k]))==1)
{
M[i,32] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Pied droit",TAit[k]))==1)
{
M[i,33] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Pied gauche",TAit[k]))==1)
{
M[i,34] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Talon droit",TAit[k]))==1)
{
M[i,35] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Talon gauche",TAit[k]))==1)
{
M[i,36] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Poitrine",TAit[k]))==1)
{
M[i,37] <- gsub("\\D","",TAnb[k])
}
if(length(grep("TĂŞte",TAit[k]))==1)
{
M[i,38] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Autres",TAit[k]))==1)
{
M[i,39] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Six mètres",TAit[k]))==1)
{
M[i,40] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Surface",TAit[k]))==1)
{
M[i,41] <- gsub("\\D","",TAnb[k])
}
if(length(grep("En dehors de la surface",TAit[k]))==1)
{
M[i,42] <- gsub("\\D","",TAnb[k])
}
if(length(grep("Moitié de terrain adverse",TAit[k]))==1)
{
M[i,43] <- gsub("\\D","",TAnb[k])
}
}
### Nombre de clubs ###
NC <- xpathSApply(parse2,
"//td[@class='club']",
xmlValue)
M[i,44] <- as.numeric(length(NC))
}
}
}
if(typeof(try(parse <- htmlParse(url2,encoding = "UTF-8"),silent=TRUE))!="externalptr")
{
for(l in 8:44){M[i,l] <- 0}
}
}
Matrice_joueurs <- cbind(J,M)
############# Enregistrer les données ##############
if(exists("Matrice_joueurs")){
write.table(Matrice_joueurs, file = "Z:\\Pomper des pages web\\LFP\\Saison_2011-2012\\Infos_joueurs_Ligue1.txt",
append = FALSE, quote = FALSE, sep = ";",
eol = "\n", na = "NA", dec = ".", row.names = FALSE,
col.names = TRUE, qmethod = c("escape"))} |