Created
June 15, 2016 18:33
-
-
Save anonymous/c926cd96dc05099d0fe6ca865e90fba3 to your computer and use it in GitHub Desktop.
Scraping NFL data with purrr and tidyr goodness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Replicating https://t.co/Jq1QfFGpjA | |
library(rvest) | |
library(stringr) | |
library(dplyr) | |
library(tidyr) | |
library(purrr) | |
library(lubridate) | |
get_and_clean_table <- function(url) { | |
paste0("http://www.pro-football-reference.com", url) %>% | |
read_html() %>% | |
html_nodes("table#game_logs") %>% | |
html_table() %>% | |
first() %>% | |
set_names(tolower(names(.))) %>% | |
filter(year != "Year") %>% | |
mutate(game = str_replace(game, "\\*", "")) %>% | |
separate(game, c("away", "home"), sep = " @ ") %>% | |
mutate_each(funs(as.integer), vpts:hpyds) %>% | |
mutate(year = ymd(year)) | |
} | |
## IO | |
officials <- read_html("http://www.pro-football-reference.com/officials/") %>% | |
html_nodes("table a") %>% | |
{data_frame(name = html_text(.), url = html_attr(., "href"))} %>% | |
mutate(data = url %>% map(get_and_clean_table)) %>% | |
unnest() | |
write_csv(officials, "officials_data.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment