-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFootballScraper.R
More file actions
372 lines (278 loc) · 8.45 KB
/
FootballScraper.R
File metadata and controls
372 lines (278 loc) · 8.45 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
install.packages('jsonlite')
install.packages('rvest')
install.packages('tidyverse')
install.packages('lubridate')
install.packages('httr')
library(jsonlite)
library(httr)
library(rvest)
library(tidyverse)
library(lubridate)
# Marktwerte scrapen
url = "https://www.transfermarkt.de/bundesliga/marktwertaenderungen/wettbewerb/L1/plus//galerie/0?verein_id=&land_id=0&pos=&detailpos="
url %>%
read_html() %>%
html_elements('.responsive-table') %>%
html_elements('.inline-table') %>%
html_elements('.hauptlink') %>%
html_elements('a') %>%
html_attr('title') -> player_name
player_name[1]
url %>%
read_html() %>%
html_elements('.responsive-table') %>%
html_elements('.zentriert') %>%
html_elements('a') %>%
html_attr('title') -> player_verein
player_verein[2]
url %>%
read_html() %>%
html_elements('.responsive-table') %>%
html_elements('.rechts.hauptlink') %>%
html_text() %>%
str_extract("[0-9]*") -> marktwert
length(marktwert)
marktwert[1] = 0.5
marktwert
length(player_name)
length(player_verein)
length(marktwert)
# marktwerte über die Zeit von 2010 bis heute
# ?stichtag=2010-11-01
url_new = "https://www.transfermarkt.de/bundesliga/marktwerteverein/wettbewerb/L1/plus/"
url_new %>%
read_html() %>%
html_elements('.inline-select') %>%
html_elements('option') %>%
html_attr('value') -> dates
dates
list_marktwert = c()
list_verein = c()
list_date = c()
for (date in dates) {
print(date)
url_new_new = paste0(url_new, '?stichtag=', date)
url_new_new %>%
rvest::read_html() %>%
html_elements('div') %>%
html_elements('.responsive-table .items tbody [class=rechts] a') -> element
element %>%
html_text() %>%
str_extract("[0-9]*\\,[0-9]*") -> items_marktwert
element %>%
html_attr('title') -> verein
# change ',' to '.'
for (i in 1:length(items_marktwert)) {
items_marktwert[i] = gsub(',', '.', items_marktwert[i])
}
list_marktwert = append(list_marktwert, items_marktwert)
list_verein = append(list_verein, verein)
list_date = append(list_date, rep(c(date), each=18))
}
list_marktwert
list_verein
list_date
df = data.frame(verein=list_verein, marktwert=list_marktwert, date=list_date)
df
readr::write_csv(df, file = 'bundesliga_marktwerte_2010_2022.csv')
# Let us now scrape all ids of all countries (total: 252)
get_all_country_ids = function(max_) {
base_url = "https://www.transfermarkt.de/wettbewerbe/national/wettbewerbe/"
list_countries = list()
list_slugs = c()
list_names = c()
list_ids = c()
i = 1
id = 1
while (i < max_) {
new_url = paste0(base_url, id)
res = httr::GET(new_url)
status = httr::status_code(res)
if (status == 200) {
i = i + 1
new_url %>%
read_html() %>%
html_elements('.box .clearer.relevante-wettbewerbe-auflistung') %>%
html_element('a') -> element_country_NEW
element_country_NEW %>%
html_attr('title') -> title
element_country_NEW %>%
html_attr('href') %>%
strsplit('/') -> slug
print(length(slug))
if (length(slug) != 0) {
slug = slug[[1]][2]
} else {
slug = NaN
}
print(length(title))
if (length(title) == 0) {
title = NaN
}
print(title)
print(id)
list_slugs = append(list_slugs, slug)
list_names = append(list_names, title)
list_ids = append(list_ids, id)
id = id + 1
} else {
print(sprintf('For id number %d no country could be found!!!', id))
id = id + 1
}
}
list_countries[[1]] = list_ids
list_countries[[2]] = list_names
list_countries[[3]] = list_slugs
list_countries
}
country_id = get_all_country_ids(252)
print(country_id)
df_countries = data.frame(id=country_id[[1]], names=country_id[[2]], slug=country_id[[3]])
df_countries
readr::write_csv(df_countries, file = 'country_codes.csv')
# Let us now scrape all ids of all european countries (total: ?)
get_all_european_country_ids = function() {
base_url = "https://www.transfermarkt.de/wettbewerbe/europa"
list_countries = list()
list_names = c()
list_ids = c()
base_url %>%
read_html() %>%
html_elements('div .content.text-rechts #europa_Map area') -> elements
elements %>%
html_attr('href') %>%
str_extract('[0-9]+') -> id
elements %>%
html_attr('title') -> title
print(title)
print(id)
list_names = append(list_names, title)
list_ids = append(list_ids, id)
list_countries[[1]] = list_ids
list_countries[[2]] = list_names
list_countries
}
test = get_all_european_country_ids()
print(test)
df_euro = data.frame(id_code=test[[1]], country=test[[2]])
df_euro
write_csv(df_euro, file='euro_country_codes.csv')
# Let us now scrape all ids of bundesliga
get_all_club_ids_bundesliga = function() {
base_url = 'https://www.transfermarkt.de/bundesliga/startseite/wettbewerb/L1'
list_bundesliga = list()
list_names = c()
list_ids = c()
base_url %>%
rvest::read_html() %>%
html_elements('.responsive-table .grid-view .items tbody [class="zentriert no-border-rechts"] a') -> elements
elements %>%
html_attr('title') -> titles
elements %>%
html_attr('href') %>%
str_extract("[/][0-9]*[/]") %>%
str_extract("[0-9]+") -> id
print(titles)
print(id)
list_bundesliga[[1]] = id
list_bundesliga[[2]] = titles
list_bundesliga
}
club_id_bundesliga = get_all_club_ids_bundesliga()
print(club_id_bundesliga)
df_bundesliga = data.frame(club_id=club_id_bundesliga[[1]], club_name=club_id_bundesliga[[2]])
df_bundesliga
# scrape all ids of FC Bayern München
get_all_players_of_club = function(club_id, season) {
base_url = paste0("https://www.transfermarkt.de/fc-bayern-munchen/startseite/verein/", club_id, "/saison_id/", season)
list_player = list()
base_url %>%
read_html() -> html_
html_ %>%
html_elements('.items tbody .rn_nummer') %>%
html_text() -> rueckennummer
html_ %>%
html_elements('.items tbody .posrela .hauptlink .hide-for-small a') %>%
html_text() -> name
html_ %>%
html_elements('.items tbody .posrela tr:last-child td') %>%
html_text() -> position
html_ %>%
html_elements('.items tbody [class="zentriert"]') -> birth_html
birth = c()
for (item in birth_html) {
if (length(html_children(item)) == 0) {
birth = append(birth, html_text(item))
}
}
print(birth)
birth %>%
str_extract('[0-9]+[.][0-9]+[.][0-9]+') -> birth_date
birth %>%
str_extract('[(][0-9]+[)]') %>%
str_extract('[0-9]+') -> age
html_ %>%
html_elements(' .items tbody [class="zentriert "]') -> nationality_html
nationality = c()
for (i in 1:length(nationality_html)) {
n = html_elements(nationality_html[[i]], 'img') %>% html_attr('title')
if(length(n) == 1) {
nationality = append(nationality, n)
} else {
nationality = append(nationality, paste0(n[1], '/', n[2]))
}
}
html_ %>%
html_elements('.items tbody [class="rechts hauptlink"]') -> marktwerte_html
print(html_elements(marktwerte_html[[11]], 'a') %>% html_text())
marktwerte = c()
for (i in 1:length(marktwerte_html)) {
n = html_elements(marktwerte_html[[i]], 'a') %>% html_text()
if(length(n) == 0) {
marktwerte = append(marktwerte, NaN)
} else {
marktwerte = append(marktwerte, n)
}
}
print(length(rueckennummer))
print(length(name))
print(length(position))
print(length(birth))
print(length(birth_date))
print(length(age))
print(length(nationality))
print(marktwerte)
list_player[[1]] = rueckennummer
list_player[[2]] = name
list_player[[3]] = position
list_player[[4]] = birth_date
list_player[[5]] = age
list_player[[7]] = nationality
list_player[[8]] = marktwerte
list_player
}
fc_bayern_munchen = get_all_players_of_club(15, 2021)
df_fc_bayern_player = data.frame(
rueckennummer=fc_bayern_munchen[[1]],
name=fc_bayern_munchen[[2]],
pos=fc_bayern_munchen[[3]],
birth_date=fc_bayern_munchen[[4]],
age=fc_bayern_munchen[[5]],
nationalities=fc_bayern_munchen[[7]],
marktwerte=fc_bayern_munchen[[8]]
)
df_fc_bayern_player
write_csv(df_fc_bayern_player, file='fc_bayern_munchen_players.csv')
eintracht_frankfurt = get_all_players_of_club(15, 2021)
df_eintracht_frankfurt_player = data.frame(
rueckennummer=eintracht_frankfurt[[1]],
name=eintracht_frankfurt[[2]],
pos=eintracht_frankfurt[[3]],
birth_date=eintracht_frankfurt[[4]],
age=eintracht_frankfurt[[5]],
nationalities=eintracht_frankfurt[[7]],
marktwerte=eintracht_frankfurt[[8]]
)
df_eintracht_frankfurt_player
# test/test
### Testing Github