Skip to content

Instantly share code, notes, and snippets.

@ellisvalentiner
Last active October 22, 2016 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ellisvalentiner/5ea4aa508a5c2638c298af98fbac1ed2 to your computer and use it in GitHub Desktop.
Save ellisvalentiner/5ea4aa508a5c2638c298af98fbac1ed2 to your computer and use it in GitHub Desktop.
library(rvest)
library(dplyr)
library(tidyr)
library(readr)
library(jsonlite)
library(lubridate)
### Begin scrape ###
polls <- xml2::read_html("http://projects.fivethirtyeight.com/2016-election-forecast/national-polls/") %>%
rvest::html_nodes(xpath = "//script[contains(., 'race.model')]") %>%
gsub(".*race.stateData = (.*);race.pathPrefix.*", "\\1", .) %>%
jsonlite::fromJSON(., flatten = TRUE) %>%
magrittr::extract2("polls") %>%
tidyr::unnest(votingAnswers) %>%
readr::type_convert()
### End scrape ###
### Example spread ###
polls %>%
select(id, population, sampleSize, state, startDate, endDate, choice, pct) %>%
tidyr::spread(choice, pct) %>%
arrange(desc(endDate)) %>%
slice(1:10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment