forked from ColinJoMurphy/NBA-Dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrapeAndWriteData.R
88 lines (64 loc) · 2.91 KB
/
ScrapeAndWriteData.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
##### README #####
#' The following script scrapes 2021/2022 NBA season data from
#' basketball-reference.com. Specifically, the script scrapes the 'Advanced Stats'
#' and 'Shooting Stats' for team data and scrapes 'Total Stats' for individual players.
#'
#' The data is lightly cleaned, e.g. removing empty columns, and saved to a SQL
#' database using IBM's Db2 on Cloud service.
##### Packages #####
library(data.table)
library(tidyr)
library(RODBC)
library(xml2)
library(rvest)
##### Define Web Data #####
webdata <- data.table('dataname' = c('PLAYER_DATA',
'TEAM_ADVANCED_DATA',
'TEAM_SHOOTING'),
'urlsource' = c('https://www.basketball-reference.com/leagues/NBA_2022_totals.html',
'https://www.basketball-reference.com/leagues/NBA_2022.html',
'https://www.basketball-reference.com/leagues/NBA_2022.html'),
'tableselector' = c('#totals_stats > tbody',
'#advanced-team > tbody',
'#shooting-team > tbody'),
'headerselector' = c('#totals_stats > thead > tr',
'#advanced-team > thead > tr:nth-child(2)',
'#shooting-team > thead > tr:nth-child(2)')
)
##### Scrape Function #####
nba.scrap <- function(webdata){
stopifnot(is.data.table(webdata))
scraped <- list()
statkeys <- list()
for (row in 1:nrow(webdata)){
webtable <- webdata[row, urlsource] %>%
read_html() %>%
html_elements(css = webdata[row, tableselector]) %>%
html_table() %>%
as.data.table()
webtablekey <- webdata[row, urlsource] %>%
read_html() %>%
html_elements(css = webdata[row, headerselector]) %>%
html_children()
vars <- sapply(webtablekey, function(x) {x <- html_attr(x,'data-stat') %>%
tolower()})
setnames(webtable, names(webtable), vars)
webtable[ , which(names(webtable) == 'dummy') := NULL]
webtablekey <- sapply(webtablekey, function(x) {n <- html_attr(x,'aria-label')})
k <- data.table('stat' = vars, 'description' = webtablekey)
k <- k[stat != 'dummy']
tablename <- webdata[row, dataname]
scraped[[tablename]] <- webtable
keyname <- paste0(webdata[row, dataname], "_KEY")
statkeys[[keyname]] <- k
}
}
##### Write Data To Db2 #####
# Connect using RODBC package
con <- odbcConnect('DB2', 'fjs08406', 'hVJHsH2WnvB9H3Bm')
for (dname in webdata$dataname){
sqlSave(con, scraped[[dname]], tablename = dname)
keyname <- paste0(dname, "_KEY")
sqlSave(con, statkeys[[keyname]], tablename = keyname)
}
closeAllConnections()