-
Notifications
You must be signed in to change notification settings - Fork 7
/
scrape_colours.R
132 lines (123 loc) · 3.64 KB
/
scrape_colours.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
##
## scrape_players: players in squads
## scrape_flag: national team flags
## scrape_comp: competition teams and logos
## scrape_colours: national team kits
##
library(tidyverse)
library(rvest)
library(countrycode)
d <- read_csv("./data/wiki_comp.csv") %>%
select(team_alpha3, url_team) %>%
distinct()
get_kit_colours <- function(u){
# u = d$url_team[14]
h <- paste0("https://en.wikipedia.org/", u) %>%
read_html()
#kit colour
tibble(
colours = h %>%
html_nodes(".infobox table td:nth-child(1) div:nth-child(1)") %>%
# html_nodes(".infobox table td:nth-child(1) div:nth-child(1) div:nth-child(1)") %>%
# html_attr("style") %>%
as.character() %>%
paste0(collapse = ";") %>%
str_split(";|[>]") %>%
.[[1]] %>%
str_subset("background-color") %>%
str_remove("background-color:") %>%
str_trim() %>%
str_subset(".{7,}") %>%
str_sub(end = 7) %>%
unique()
)
}
d0 <- d %>%
mutate(kit = map(.x = url_team, .f = ~get_kit_colours(u = .x)))
d1 <-
d0 %>%
unnest(kit) %>%
group_by(url_team) %>%
mutate(n = 1:n()) %>%
pivot_wider(names_from = n, values_from = colours, values_fill = NA,
names_prefix = "c")
write_excel_csv(x = d1, file = "./data/wiki_colours.csv")
# d0 <- read_csv("./data/wiki_colours.csv")
# pie(rep(1, nrow(d0)), labels = d0$team_alpha3, col = d0$kit_shirt)
# h <- read_html("https://en.wikipedia.org/wiki/National_colours")
#
# d <- tibble(
# country = h %>%
# # html_table(fill = TRUE)
# html_nodes("table") %>%
# .[1:14] %>%
# html_nodes("td:nth-child(1)") %>%
# html_text(trim = TRUE),
# colour1_name = h %>%
# html_nodes("table") %>%
# .[1:14] %>%
# html_nodes("td:nth-child(3)") %>%
# html_text(trim = TRUE),
# colour2_name = h %>%
# html_nodes("table") %>%
# .[1:14] %>%
# html_nodes("td:nth-child(4)") %>%
# html_text(trim = TRUE)
# )
# c1 <- h %>%
# html_nodes("table") %>%
# .[1:14] %>%
# html_nodes("td:nth-child(5)")
# c2 <- h %>%
# html_nodes("table") %>%
# .[1:14] %>%
# html_nodes("td:nth-child(6)")
#
# get_colour_code <- function(x){
# x %>%
# html_nodes(".legend-color") %>%
# as.character() %>%
# str_extract_all(pattern = "background-color:\\s*(.*?)\\s*;") %>%
# unlist() %>%
# str_remove(pattern = "background-color:") %>%
# str_remove(pattern = ";") %>%
# paste(collapse = ",")
# }
#
#
# d$colour1_code <- NA
# d$colour2_code <- NA
# for(i in 1:nrow(d)){
# d$colour1_code[i] <- get_colour_code(x = c1[i])
# d$colour2_code[i] <- get_colour_code(x = c2[i])
# }
#
# cm <- c("CIS" = "CIS",
# "CSSR" = "CSK",
# "England" = "GB-ENG",
# "Northern Ireland" = "GB-NIR",
# "Scotland" = "GB-SCT",
# "Wales" = "GB-WLS",
# "Yugoslavia" = "YUG",
# "Soviet Union" = "SUN",
# "USSR" = "SUN")
# d <- d %>%
# mutate(alpha3 = countrycode(sourcevar = country,
# origin = "country.name",
# destination = "iso3c",
# custom_match = cm))
#
# write_excel_csv(x = d, file = "./data/wiki_colours.csv")
#
# dd <- read_csv(file = "./data/wfnet_players.csv") %>%
# select(year, team, alpha3) %>%
# distinct() %>%
# left_join(d) %>%
# arrange(team) %>%
# select(-contains("name")) %>%
# separate(col = colour1_code, into = c("col1", "col2"),
# remove = FALSE, extra = "drop", fill = "right") %>%
# filter(!str_detect(string = country, pattern = "[0-9]"))
#
# write_excel_csv(x = dd, file = "./data/wiki_colours2.csv")
# # manually edit in colours_team.xlxs the team colours