-
Notifications
You must be signed in to change notification settings - Fork 16
/
scrape_players.R
165 lines (147 loc) · 4.88 KB
/
scrape_players.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
##
## scrape_players: players in squads
## scrape_flag: national team flags
## scrape_colours: national team kits
## scrape_comp: competition teams and logos
##
library(tidyverse)
library(rvest)
library(countrycode)
library(janitor)
d <- tibble(
year = c(1930, 1934, 1938, seq(1950, 2018, 4)),
teams = c(13, 16, 15, 13, rep(16, 7), rep(24, 4), rep(32, 6)),
url = paste0("https://en.wikipedia.org/wiki/", year,"_FIFA_World_Cup_squads")
)
get_players <- function(u, n){
# u = d$url[i];u
# n = d$teams[i]
h <- read_html(u)
hh <- h %>%
html_nodes("table.sortable")
p <- tibble(
squads = hh %>%
html_table()
) %>%
mutate(nn = map(.x = squads, .f = ~nrow(x = .x)),
nn = unlist(nn),
squad_list = nn >= 11,
table_no = cumsum(squad_list),
squad_list = ifelse(table_no > n, FALSE, squad_list))
s <- h %>%
html_nodes(".mw-headline") %>%
html_text() %>%
str_subset(pattern = "Group", negate = TRUE) %>%
.[1:n]
tibble(
squad = s,
player = p$squads[p$squad_list]
) %>%
mutate(
player = map(
.x = player,
.f = ~mutate(.x, across(everything(), as.character))
)) %>%
unnest(cols = player) %>%
filter(Player != "") %>%
mutate(player_url = hh %>%
.[p$squad_list] %>%
html_nodes(".nat-fs-player th") %>%
html_node("a") %>%
html_attr("href"),
club_fa_url = hh %>%
.[p$squad_list] %>%
html_nodes("td:nth-last-child(1)") %>%
html_node("a") %>%
html_attr("href"),
club_fa = hh %>%
.[p$squad_list] %>%
html_nodes("td:nth-last-child(1)") %>%
html_node("a") %>%
html_attr("title"),
club = hh %>%
.[p$squad_list] %>%
html_nodes("td:nth-last-child(1)") %>%
html_text(trim = TRUE),
club_country = hh %>%
.[p$squad_list] %>%
html_nodes("td:nth-last-child(1)") %>%
html_node(".flagicon") %>%
html_node(".thumbborder") %>%
html_attr("alt"),
club_country_flag = hh %>%
.[p$squad_list] %>%
html_nodes("td:nth-last-child(1)") %>%
html_node(".flagicon") %>%
html_node("img") %>%
html_attr("src")
)
}
d0 <- d %>%
mutate(players = map2(.x = url, .y = teams,
.f = ~get_players(u = .x, n = .y)))
# for(i in 1:nrow(d))
# get_players(u = d$url[i], n = d$teams[i])
d1 <- d0 %>%
unnest(cols = c("players")) %>%
clean_names() %>%
select(-url, -teams) %>%
mutate(pos = str_sub(string = pos, start = 2),
captain = str_detect(string = player, pattern = "captain|Captain|\\(c\\)"),
player_original = player,
player = str_remove(string = player, pattern = " \\s*\\([^\\)]+\\)"))
cm <- c("CSSR" = "CSK",
"Czechoslovakia" = "CSK",
"England" = "GB-ENG",
"Northern Ireland" = "GB-NIR",
"Scotland" = "GB-SCT",
"Wales" = "GB-WLS",
"Serbia and Montenegro" = "SCG",
"FR Yugoslavia" = "SCG",
"Yugoslavia" = "YUG",
"Kingdom of Yugoslavia" = "YUG",
"Federal Republic of Yugoslavia" = "YUG",
"USSR" = "SUN",
"Soviet Union" = "SUN",
"East Germany" = "DDR",
"Dutch East Indies" = "DEI")
# d1 <- read_csv("./data/wiki_players.csv", n_max = 1e6)
d2 <- d1 %>%
mutate(
nat_team = case_when(
squad == "Soviet Union" ~ "USSR",
squad == "China PR" ~ "China",
TRUE ~ squad
),
club_country_harm = case_when(
club_country == "Wales" ~ "England",
club_country == "Soviet Union" ~ "USSR",
club_country == "Socialist Federal Republic of Yugoslavia" ~ "Yugoslavia",
club_country == "Kingdom of Yugoslavia" ~ "Yugoslavia",
club_country == "Federal Republic of Yugoslavia" ~ "Serbia and Montenegro",
TRUE ~ club_country),
nat_team_alpha3 = countrycode(
sourcevar = nat_team, origin = "country.name",
destination = "iso3c", custom_match = cm),
club_alpha3 = countrycode(
sourcevar = club_country_harm, origin = "country.name",
destination = "iso3c", custom_match = cm),
club_country_flag = str_remove(string = club_country_flag, pattern = "thumb"),
club_country_flag = str_remove(string = club_country_flag, pattern = "/[^/]+$")
)
# checks on nat_team and club_country_harm match
d2 %>%
# filter(is.na(nat_team))
# filter(is.na(nat_team_alpha3))
# filter(is.na(club_country_harm))
filter(is.na(club_alpha3))
x = d2 %>%
select(nat_team, club_country_harm) %>%
pivot_longer(cols = 1:2, names_to = "type", values_to = "label") %>%
select(-type) %>%
distinct() %>%
arrange(label)
# group_by(label) %>%
# mutate(n = n()) %>%
# arrange(desc(n))
write_excel_csv(d2 , "./data/wiki_players.csv")