library(understatr)
get_league_teams_stats('EPL', 2020)
#> # A tibble: 760 x 25
#> h_a xG xGA npxG npxGA deep deep_allowed scored missed xpts result
#> <chr> <dbl> <dbl> <dbl> <dbl> <int> <int> <int> <int> <dbl> <chr>
#> 1 h 0.805 0.850 0.805 0.0885 17 2 1 0 1.16 w
#> 2 a 2.03 0.535 2.03 0.535 10 5 3 0 2.46 w
#> 3 h 3.08 1.66 3.08 1.66 7 18 7 2 2.26 w
#> 4 a 0.874 0.672 0.874 0.672 7 4 1 0 1.53 w
#> 5 h 1.50 2.38 1.50 2.38 7 20 0 3 0.824 l
#> 6 h 2.45 1.00 1.69 1.00 5 2 3 4 2.39 l
#> 7 a 1.99 1.39 1.99 1.39 16 6 3 0 1.81 w
#> 8 h 1.77 1.50 1.77 1.50 6 4 1 2 1.62 l
#> 9 a 2.39 0.572 1.63 0.572 8 2 1 2 2.68 l
#> 10 a 1.27 1.14 0.508 1.14 5 6 1 0 1.49 w
#> # ... with 750 more rows, and 14 more variables: date <date>, wins <int>,
#> # draws <int>, loses <int>, pts <int>, npxGD <dbl>, ppda.att <int>,
#> # ppda.def <int>, ppda_allowed.att <int>, ppda_allowed.def <int>,
#> # team_id <chr>, team_name <chr>, league_name <chr>, year <dbl>
get_league_teams_stats('La liga', 2020)
#> Error in open.connection(x, "rb"): HTTP error 400.
library(stringr)
library(rvest)
library(jsonlite)
library(tibble)
get_league_teams_stats2 <- function(league_name, year) {
stopifnot(is.character(league_name))
home_url <- "https://understat.com"
# construct league url
league_url <- URLencode(stringr::str_glue("{home_url}/league/{league_name}/{year}"))
# read league page
league_page <- rvest::read_html(league_url)
# locate script tags
teams_data <- understatr:::get_script(league_page)
# isolate player data
teams_data <- understatr:::get_data_element(teams_data, "teamsData")
# pick out JSON string
teams_data <- sub(".*?\\'(.*)\\'.*", "\\1", teams_data)
# parse JSON
teams_data <- jsonlite::fromJSON(teams_data, simplifyDataFrame = TRUE,
flatten = TRUE)
# get teams data
teams_data <- lapply(
teams_data, function(x) {
df <- x$history
df$team_id <- x$id
df$team_name <- x$title
df
})
# convert to df
teams_df <- do.call("rbind", teams_data)
# add reference fields
teams_df$league_name <- league_name
teams_df$year <- as.numeric(year)
# fix col classes
teams_df$date <- as.Date(teams_df$date, "%Y-%m-%d")
tibble::as_tibble(teams_df)
}
get_league_teams_stats2('La liga', 2020)
#> # A tibble: 760 x 25
#> h_a xG xGA npxG npxGA deep deep_allowed scored missed xpts result
#> <chr> <dbl> <dbl> <dbl> <dbl> <int> <int> <int> <int> <dbl> <chr>
#> 1 a 2.21 1.10 2.21 1.10 13 3 3 1 2.15 w
#> 2 h 1.05 0.375 1.05 0.375 8 0 1 0 1.95 w
#> 3 a 1.26 1.06 1.26 1.06 2 3 1 1 1.53 d
#> 4 a 0.699 1.09 0.699 1.09 5 5 0 1 0.946 l
#> 5 h 1.99 0.198 1.99 0.198 5 2 0 1 2.74 l
#> 6 a 1.15 1.33 1.15 1.33 6 3 1 2 1.17 l
#> 7 h 1.15 0.499 0.408 0.499 3 5 1 0 2.06 w
#> 8 h 2.23 2.08 2.23 2.08 5 3 4 2 1.46 w
#> 9 a 1.70 0.168 1.70 0.168 11 2 1 0 2.65 w
#> 10 h 0.534 1.70 0.534 1.70 6 4 0 1 0.397 l
#> # ... with 750 more rows, and 14 more variables: date <date>, wins <int>,
#> # draws <int>, loses <int>, pts <int>, npxGD <dbl>, ppda.att <int>,
#> # ppda.def <int>, ppda_allowed.att <int>, ppda_allowed.def <int>,
#> # team_id <chr>, team_name <chr>, league_name <chr>, year <dbl>