ntbloom / gamewinner

college bball bracket predictor -- bring your own algo
GNU General Public License v3.0
1 stars 1 forks source link

Scraper #22

Open michaelmcd18 opened 1 year ago

michaelmcd18 commented 1 year ago
standings_up_to_date <-function(date, division) {
  
  url <- paste0("http://www.baseball-reference.com/boxes",
                "?year=", sprintf("%04i", lubridate::year(date)), "&month=",
                sprintf("%02i", lubridate::month(date)), "&day=", sprintf("%02i",
                                                                          lubridate::day(date)))
  
  tabls <- read_html(url) %>%
    rvest::html_elements("table")
  
  minT <- length(tabls)
  maxT <- length(tabls) - 15
  
  all_tabls <- tabls[minT:maxT] %>% html_table()
  
  table_names <- c("NL Overall", "AL Overall", "NL West" , "NL Central", "NL East", "AL West", "AL Central", "AL East", "NL Overall", "AL Overall", "NL West" , "NL Central", "NL East", "AL West", "AL Central", "AL East")
  table_names[1:8] <- paste0(table_names[1:8], "_after_", date)     # Customizing list names for "After this Date" case
  table_names[9:16] <- paste0(table_names[9:16], "_up to_", date)   # Customizing list names for "From this Date" case
  
  names(all_tabls) <- table_names
  
  current_tables <- all_tabls[9:16]
  
  div_date <- paste0(division, "_up to_", date)
  x <- current_tables[div_date][[1]]
  return(x)
  
}