Demo entry 3549061

r

   

Submitted by anonymous on Jan 21, 2016 at 02:39
Language: S. Code size: 1.3 kB.

transfermarkt <- function (x = dara.url){
  # Create data.table for team loops
  team.value <- data.table()
  for (j in 1:length(x)){
    # Initial pull with all the liga MX names-url information
    all.ligamx <- read_html(paste0(url.t,x[j])) %>%
      html_nodes("table.items") %>%
      .[1]
    # Get teams url
    team.url <- html_nodes(all.ligamx, "a") %>%
      html_attr("href")
    team.url <- unique(team.url[team.url %like% "startseite"])
    # Get teams names
    names <- html_table(all.ligamx, fill = TRUE) %>%
      as.data.frame() %>%
      .[-1,"Club"]
    # Create data.frame with url-names
    db <- cbind.data.frame(team.url, names)

    # Get each player´s cost by team
    for (i in 1:nrow(db)){
      temp <- read_html(paste0(url.t,db[i,1])) %>%
        html_nodes("table.items") %>%
        .[1] %>%
        html_table(fill = TRUE) %>%
        as.data.frame()
      temp$team <- db[i,2]
      team.value <- rbind.data.frame(team.value, temp)
    }
  }
  # Update values
  headers <- c("number","x1","x2","x3","position","name","age","x7","value","team")
  names(team.value) <- headers
  team.value <- subset(team.value,
                       select = c("number","position","name","age","value","team"))
  return(team.value)
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).