Skip to content

Instantly share code, notes, and snippets.

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 briatte/d55f79b307ca54c2b43f8a97501ad42f to your computer and use it in GitHub Desktop.
Save briatte/d55f79b307ca54c2b43f8a97501ad42f to your computer and use it in GitHub Desktop.
Scrape American Presidency Project Approval Data
library(rvest)
library(lubridate)
library(tidyverse)
Truman <- read_html("http://www.presidency.ucsb.edu/data/popularity.php?pres=33")
Truman %>%
html_table(fill=T) -> Truman
Truman[[11]] -> Truman
Truman %>%
select(X1:X3, X5:X7) %>%
slice(7:n()) -> Truman
for (i in 34:45) {
url <- paste0("http://www.presidency.ucsb.edu/data/popularity.php?pres=",i)
url %>%
read_html() %>%
html_table(fill=TRUE) -> holdme
holdme[[11]] -> holdme
holdme %>%
select(X1:X3, X5:X7) %>%
slice(7:n()) -> holdme
Truman <- rbind(Truman, holdme)
}
Truman %>%
setNames(., c("potus", "stdate", "enddate", "approve", "disapprove", "unsure")) %>%
mutate(stdate = mdy(stdate),
enddate = mdy(enddate),
potus = ifelse(potus == "", NA, potus),
approve = as.integer(approve),
disapprove = as.integer(disapprove),
unsure = as.integer(unsure),
netapprove = approve - disapprove) %>%
tidyr::fill(potus) %>% arrange(stdate) -> Approval
write_csv(Approval, "potus-approval.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment