Scrape your personal bikeshare trip data with R
Last active
February 17, 2016 18:21
-
-
Save sachsmc/5f51871ad3791739df78 to your computer and use it in GitHub Desktop.
Scrape your personal Capital Bikeshare Trip data
This file contains 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
license: mit |
This file contains 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
library(rvest) | |
s <- html_session("https://secure.capitalbikeshare.com/profile/login") | |
s2 <- s %>% html_form %>% `[[`(., 1) %>% | |
set_values(`_username` = "username", `_password` = "*********") %>% | |
submit_form(s, .) | |
get_trip_info <- function(item, trips, trim = TRUE){ | |
node <- paste0(".ed-table__item__info_trip-", item) | |
trips %>% html_nodes(css = node) %>% html_text(trim = trim) | |
} | |
myinfo <- c("start-station", "end-station", "duration", "cost", "start-date") | |
alldat <- NULL | |
for(i in 1:40){ | |
trips <- s2 %>% html_node(".ed-profile-menu__link_trips > a") %>% html_attr("href") %>% | |
paste0("?pageNumber=", i) %>% jump_to(s2, .) | |
infolist <- as.data.frame(lapply(myinfo, get_trip_info, trips)) | |
colnames(infolist) <- myinfo | |
alldat <- rbind(alldat, infolist) | |
} | |
alldat$timesec <- sapply(strsplit(paste(alldat$duration), " ", fixed = TRUE), function(v){ | |
as.numeric(v[1]) * 60 + as.numeric(v[3]) | |
}) | |
alldat$Date <- as.Date(sapply(strsplit(paste(alldat$`start-date`), "\\s+"), | |
function(x){ x[2] }), format = "%Y/%m/%d") |
This file contains 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
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<meta name="generator" content="pandoc" /> | |
<title></title> | |
</head> | |
<body> | |
<p><img src="unnamed-chunk-1-1.png" title="" alt="" width="960" /></p> | |
</body> | |
</html> |
Author
sachsmc
commented
Dec 10, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment