0

I'm relatively new to R and web scraping, so apologies for any inherently obvious mistakes.

I'm looking to scrape a CSV file off URL 1, increment by date to URL 2, then save each CSV file.

startdate <- as.Date("2007-07-01")
enddate <- as.Date(Sys.Date())

for(startdate in enddate){ // Loop through dates on each URL 
    read.csv(url("http://api.foo.com/charts/data?output=csv&data=close&startdate=",startdate,"&enddate=",startdate,"&exchanges=bpi&dev=1"))
    startdate = startdate + 1
    startdate <- startdate[-c(1441,1442),] // Irrelevant to question at hand. Removes unwanted information auto-inserted into CSV. 
    write.csv(startdate[-c(1441,1442),], startdate, 'csv', row.names = FALSE)
}

The following errors are being outputted:

read.csv(url("http://api.foo.com/charts/data?output=csv&data=close&startdate=",startdate,"&enddate=",startdate,"&exchanges=bpi&dev=1"))
// Error in match.arg(method, c("default", "internal", "libcurl", "wininet")) :'arg' should be one of “default”, “internal”, “libcurl”, “wininet”

and:

write.csv(startdate[c(1441,1442),], startdate, 'csv', row.names = FALSE)
//Error in charToDate(x) : character string is not in a standard unambiguous format

Any suggestions on how to fix these errors?

Spacedman
  • 92,590
  • 12
  • 140
  • 224
rsylatian
  • 429
  • 2
  • 14

1 Answers1

1

based on your objective "I'm looking to scrape a CSV file off URL 1, increment to URL 2 by date, then save each CSV file." here is an example code:

startdate <- as.Date("2016-01-01")
enddate <- as.Date(Sys.Date())

geturl <- function(sdt, edt) {
    paste0("http://api.foo.com/charts/data?output=csv&data=close",
        "&startdate=",sdt,"&enddate=",edt,"&exchanges=bpi&dev=1")
} #geturl

dir.create("data")
garbage <- lapply(seq.Date(startdate, enddate, by="1 day"), function(dt) {
    dt <- as.Date(dt)
    dat <- read.csv(url(geturl(dt, dt)))
    write.csv(dat, paste0("data/dat-",format(dt, "%Y%m%d"),".csv"), row.names=FALSE)
})

is this what you are looking for? can you provide a sample link? and some sample dates?

chinsoon12
  • 25,005
  • 4
  • 25
  • 35