-
Notifications
You must be signed in to change notification settings - Fork 0
/
roster_scrape.R
184 lines (165 loc) · 8.15 KB
/
roster_scrape.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
roster_ws <- read_html("https://www.nhl.com/news/2021-world-junior-championship-rosters/c-319801144")
wjr_rosters <- roster_ws %>% html_node(xpath = '//*[@id="content-wrap"]/div[3]/div[2]/div/article/div[4]/div[2]') %>%
html_nodes("p") %>%
html_text()
wjr_rosters <- wjr_rosters[10:315] %>%
as_tibble()
#### austria ####
roster_aut <- as_tibble(wjr_rosters[c(2:4, 6:14, 16:30), ]) %>%
separate(value, sep = ",", into = c("player", "team", "league")) %>%
add_column(position = c(rep("G", 3), rep("D", 9),rep("F", 15))) %>%
mutate(team = str_trim(team, side = "both"),
league = str_trim(league, side = "both"),
draft_eligible = str_extract(league, "2+[0-9]+"),
draft_eligible = case_when(is.na(draft_eligible) == TRUE ~ "Drafted",
TRUE ~ draft_eligible),
league = str_remove_all(league, "[:blank:]\\(.*\\)"),
league = if_else(is.na(league == TRUE), "NHL", league),
nation = "austria"
)
#### canada ####
roster_can <- as_tibble(wjr_rosters[c(34:36, 38:45, 47:60), ]) %>%
separate(value, sep = ",", into = c("player", "team", "league")) %>%
add_column(position = c(rep("G", 3), rep("D", 8),rep("F", 14))) %>%
mutate(team = str_trim(team, side = "both"),
league = str_trim(league, side = "both"),
draft_eligible = str_extract(league, "2+[0-9]+"),
draft_eligible = case_when(is.na(draft_eligible) == TRUE ~ "Drafted",
TRUE ~ draft_eligible),
league = str_remove_all(league, "[:blank:]\\(.*\\)"),
league = if_else(is.na(league == TRUE), "NHL", league),
nation = "canada"
)
#### czech ####
roster_cze <- as_tibble(wjr_rosters[c(63:65, 67:75, 77:92), ]) %>%
separate(value, sep = ",", into = c("player", "team", "league")) %>%
add_column(position = c("G", "G", "G",
"D","D","D","D","D","D","D","D","D",
"F","F","F","F","F","F","F","F","F","F","F","F","F","F","F","F")
) %>%
mutate(team = str_trim(team, side = "both"),
league = str_trim(league, side = "both"),
draft_eligible = str_extract(league, "2+[0-9]+"),
draft_eligible = case_when(is.na(draft_eligible) == TRUE ~ "Drafted",
TRUE ~ draft_eligible),
league = str_remove_all(league, "[:blank:]\\(.*\\)"),
league = if_else(is.na(league == TRUE), "NHL", league),
nation = "czech_republic"
)
#### finland ####
roster_fin <- as_tibble(wjr_rosters[c(96:98, 100:107, 109:122), ]) %>%
separate(value, sep = ",", into = c("player", "team", "league")) %>%
add_column(position = c("G", "G", "G",
"D","D","D","D","D","D","D","D",
"F","F","F","F","F","F","F","F","F","F","F","F","F","F")
) %>%
mutate(team = str_trim(team, side = "both"),
league = str_trim(league, side = "both"),
draft_eligible = str_extract(league, "2+[0-9]+"),
draft_eligible = case_when(is.na(draft_eligible) == TRUE ~ "Drafted",
TRUE ~ draft_eligible),
league = str_remove_all(league, "[:blank:]\\(.*\\)"),
league = if_else(is.na(league == TRUE), "NHL", league),
nation = "finland"
)
#### germany ####
roster_ger <- as_tibble(wjr_rosters[c(125:127, 129:136, 138:151), ]) %>%
separate(value, sep = ",", into = c("player", "team", "league")) %>%
add_column(position = c("G", "G", "G",
"D","D","D","D","D","D","D","D",
"F","F","F","F","F","F","F","F","F","F","F","F","F","F")
) %>%
mutate(team = str_trim(team, side = "both"),
league = str_trim(league, side = "both"),
draft_eligible = str_extract(league, "2+[0-9]+"),
draft_eligible = case_when(is.na(draft_eligible) == TRUE ~ "Drafted",
TRUE ~ draft_eligible),
league = str_remove_all(league, "[:blank:]\\(.*\\)"),
league = if_else(is.na(league == TRUE), "NHL", league),
nation = "germany"
)
#### russia ####
roster_rus <- as_tibble(wjr_rosters[c(154:156, 158:165, 167:180), ]) %>%
separate(value, sep = ",", into = c("player", "team", "league")) %>%
add_column(position = c(rep("G", 3), rep("D", 8),rep("F", 14))
) %>%
mutate(team = str_trim(team, side = "both"),
league = str_trim(league, side = "both"),
draft_eligible = str_extract(league, "2+[0-9]+"),
draft_eligible = case_when(is.na(draft_eligible) == TRUE ~ "Drafted",
TRUE ~ draft_eligible),
league = str_remove_all(league, "[:blank:]\\(.*\\)"),
league = if_else(is.na(league == TRUE), "NHL", league),
nation = "russia"
)
#### slovakia ####
roster_svk <- as_tibble(wjr_rosters[c(183:186, 188:197, 199:214), ]) %>%
separate(value, sep = ",", into = c("player", "team", "league")) %>%
add_column(position = c(rep("G", 4), rep("D", 10),rep("F", 16))
) %>%
mutate(team = str_trim(team, side = "both"),
league = str_trim(league, side = "both"),
draft_eligible = str_extract(league, "2+[0-9]+"),
draft_eligible = case_when(is.na(draft_eligible) == TRUE ~ "Drafted",
TRUE ~ draft_eligible),
league = str_remove_all(league, "[:blank:]\\(.*\\)"),
league = if_else(is.na(league == TRUE), "NHL", league),
nation = "slovakia"
)
#### sweden ####
roster_swe <- as_tibble(wjr_rosters[c(218:220, 222:229, 231:244), ]) %>%
separate(value, sep = ",", into = c("player", "team", "league")) %>%
add_column(position = c(rep("G", 3), rep("D", 8),rep("F", 14))
) %>%
mutate(team = str_trim(team, side = "both"),
league = str_trim(league, side = "both"),
draft_eligible = str_extract(league, "2+[0-9]+"),
draft_eligible = case_when(is.na(draft_eligible) == TRUE ~ "Drafted",
TRUE ~ draft_eligible),
league = str_remove_all(league, "[:blank:]\\(.*\\)"),
league = if_else(is.na(league == TRUE), "NHL", league),
nation = "sweden"
)
#### switzerland ####
roster_sui <- as_tibble(wjr_rosters[c(247:249, 251:260, 262:276), ]) %>%
separate(value, sep = ",", into = c("player", "team", "league")) %>%
add_column(position = c(rep("G", 3), rep("D", 10),rep("F", 15))
) %>%
mutate(team = str_trim(team, side = "both"),
league = str_trim(league, side = "both"),
draft_eligible = str_extract(league, "2+[0-9]+"),
draft_eligible = case_when(is.na(draft_eligible) == TRUE ~ "Drafted",
TRUE ~ draft_eligible),
league = str_remove_all(league, "[:blank:]\\(.*\\)"),
league = if_else(is.na(league == TRUE), "NHL", league),
nation = "switzerland"
)
#### united_states ####
roster_usa <- as_tibble(wjr_rosters[c(280:282, 284:292, 294:306), ]) %>%
separate(value, sep = ",", into = c("player", "team", "league")) %>%
add_column(position = c(rep("G", 3), rep("D", 9),rep("F", 13))
) %>%
mutate(team = str_trim(team, side = "both"),
league = str_trim(league, side = "both"),
draft_eligible = str_extract(league, "2+[0-9]+"),
draft_eligible = case_when(is.na(draft_eligible) == TRUE ~ "Drafted",
TRUE ~ draft_eligible),
league = str_remove_all(league, "[:blank:]\\(.*\\)"),
league = if_else(is.na(league == TRUE), "NHL", league),
nation = "usa"
)
national_rosters <- bind_rows(roster_aut,
roster_can,
roster_cze,
roster_fin,
roster_ger,
roster_rus,
roster_sui,
roster_svk,
roster_swe,
roster_usa) %>%
mutate(pos_fac = case_when(position == "F" ~ "1",
position == "D" ~ "2",
position == "G" ~ "3",
TRUE ~ position)) %>%
write_csv("national_rosters.csv")