-
Notifications
You must be signed in to change notification settings - Fork 58
/
team_match_results.R
142 lines (108 loc) · 3.97 KB
/
team_match_results.R
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
#' Get FBref team match results
#'
#' Returns all game results for a team in a given season
#' Replaces the deprecated function get_team_match_results
#'
#' @param team_url the URL for the team season
#' @param time_pause the wait time (in seconds) between page loads
#'
#' @return returns a dataframe with the results of all games played by the selected team(s)
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @importFrom utils read.csv
#'
#' @export
#'
#' @examples
#' \dontrun{
#' try({
#' # for single teams:
#' man_city_url <- "https://fbref.com/en/squads/b8fd03ef/Manchester-City-Stats"
#' fb_team_match_results(man_city_url)
#' })
#' }
fb_team_match_results <- function(team_url, time_pause=3) {
# .pkg_message("Scraping team match logs...")
time_wait <- time_pause
get_each_team_log <- function(team_url, time_pause=time_wait) {
pb$tick()
# put sleep in as per new user agreement on FBref
Sys.sleep(time_pause)
team_page <- .load_page(team_url)
team_name <- sub('.*\\/', '', team_url) %>% gsub("-Stats", "", .) %>% gsub("-", " ", .)
opponent_names <- team_page %>% rvest::html_nodes(".left:nth-child(10) a") %>% rvest::html_text()
team_log <- team_page %>%
rvest::html_nodes("#all_matchlogs") %>%
rvest::html_nodes("table") %>%
rvest::html_table() %>% data.frame()
team_log$Opponent <- opponent_names
team_log <- team_log %>%
dplyr::mutate(Team_Url = team_url,
Team = team_name) %>%
dplyr::select(.data[["Team_Url"]], .data[["Team"]], dplyr::everything(), -.data[["Match.Report"]])
team_log <- team_log %>%
dplyr::mutate(Attendance = gsub(",", "", .data[["Attendance"]]) %>% as.numeric(),
GF = as.character(.data[["GF"]]),
GA = as.character(.data[["GA"]]))
return(team_log)
}
# create the progress bar with a progress function.
pb <- progress::progress_bar$new(total = length(team_url))
all_team_logs <- team_url %>%
purrr::map_df(get_each_team_log)
}
#' Get team match results
#'
#' Returns all game results for a team in a given season
#'
#' @param team_url the URL for the team season
#' @param time_pause the wait time (in seconds) between page loads
#'
#' @return returns a dataframe with the results of all games played by the selected team(s)
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @importFrom utils read.csv
#'
#' @export
#'
#' @examples
#' \dontrun{
#' try({
#' # for single teams:
#' man_city_url <- "https://fbref.com/en/squads/b8fd03ef/Manchester-City-Stats"
#' get_team_match_results(man_city_url)
#' })
#' }
get_team_match_results <- function(team_url, time_pause=3) {
# .pkg_message("Scraping team match logs...")
.Deprecated("fb_team_match_results")
time_wait <- time_pause
get_each_team_log <- function(team_url, time_pause=time_wait) {
pb$tick()
# put sleep in as per new user agreement on FBref
Sys.sleep(time_pause)
team_page <- xml2::read_html(team_url)
team_name <- sub('.*\\/', '', team_url) %>% gsub("-Stats", "", .) %>% gsub("-", " ", .)
opponent_names <- team_page %>% rvest::html_nodes(".left:nth-child(10) a") %>% rvest::html_text()
team_log <- team_page %>%
rvest::html_nodes("#all_matchlogs") %>%
rvest::html_nodes("table") %>%
rvest::html_table() %>% data.frame()
team_log$Opponent <- opponent_names
team_log <- team_log %>%
dplyr::mutate(Team_Url = team_url,
Team = team_name) %>%
dplyr::select(.data[["Team_Url"]], .data[["Team"]], dplyr::everything(), -.data[["Match.Report"]])
team_log <- team_log %>%
dplyr::mutate(Attendance = gsub(",", "", .data[["Attendance"]]) %>% as.numeric(),
GF = as.character(.data[["GF"]]),
GA = as.character(.data[["GA"]]))
return(team_log)
}
# create the progress bar with a progress function.
pb <- progress::progress_bar$new(total = length(team_url))
all_team_logs <- team_url %>%
purrr::map_df(get_each_team_log)
}