Created
November 19, 2014 01:54
-
-
Save jaehyeon-kim/356cf62b61248193db25 to your computer and use it in GitHub Desktop.
download NASDAQ stock data
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
# assumes codes are known beforehand | |
codes <- c("MSFT", "1234") | |
urls <- paste0("http://www.google.com/finance/historical?q=NASDAQ:", | |
codes,"&output=csv") | |
paths <- paste0(codes,"csv") | |
missing <- !(paths %in% dir(".", full.name = TRUE)) | |
missing | |
# simple error handling in case file doesn't exists | |
downloadFile <- function(url, path, ...) { | |
# remove file if exists already | |
if(file.exists(path)) file.remove(path) | |
# download file | |
tryCatch( | |
download.file(url, path, ...), error = function(c) { | |
# remove file if error | |
if(file.exists(path)) file.remove(path) | |
# create error message | |
c$message <- paste(substr(path, 1, 4),"failed") | |
message(c$message) | |
} | |
) | |
} | |
# wrapper of mapply | |
Map(downloadFile, urls[missing], paths[missing]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment