Skip to content

Instantly share code, notes, and snippets.

@sachsmc
Last active February 17, 2016 18:21
Show Gist options
  • Save sachsmc/5f51871ad3791739df78 to your computer and use it in GitHub Desktop.
Save sachsmc/5f51871ad3791739df78 to your computer and use it in GitHub Desktop.
Scrape your personal Capital Bikeshare Trip data
license: mit

Scrape your personal bikeshare trip data with R

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")
<!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>
@sachsmc
Copy link
Author

sachsmc commented Dec 10, 2015

tofromwork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment