Reputation: 41
I am trying to download an Excel file from Central bank of Colombia website, but it seems that the usual download.file function couldn't do the job.
For example, I am trying to download the first file "Serie historica" on this page: http://www.banrep.gov.co/es/indice-tasa-cambio-real
The link to the file is as follows, which I used in the download.file function http://obieebr.banrep.gov.co/analytics/saw.dll?Download&Format=excel2007&Extension=.xls&BypassCache=true&path=%2Fshared%2FSeries%20Estad%c3%adsticas_T%2F1.%20Indice%20de%20Tasa%20de%20Cambio%20Real%2F1.1.%20Serie%20historica_IQY&SyncOperation=1&NQUser=publico&NQPassword=publico
The command I used is:
download.file(filepath, destfile, quiet=FALSE, mode="wb")
Upvotes: 4
Views: 8429
Reputation: 2324
If you are on the windows
please consider using mode = "wb"
:
download.file("http://gapm.io/dl_pop",
destfile = "data/pop1800_2100.xlsx",
mode = "wb")
Upvotes: 2
Reputation: 11
The answer is a bit late but I think it is still worthwhile to provide a solution.
It is not possible to use the download.file function since this link does not really direct directly to the file. Actually it is a query to an API using the GET method, so you should use another code structure to get the file, a situation that can occur repeatedly for those who use webscraping techniques
I share an example of how the excel file is obtained for the daily COLCAP index:
url <- 'http://obieebr.banrep.gov.co/analytics/saw.dll?Download&Format=excel2007&Extension=.xlsx&BypassCache=true&Path=%2fshared%2fSeries%20Estad%c3%adsticas_T%2f1.%20%c3%8dndices%20de%20mercado%20burs%c3%a1til%20colombiano%2f1.1.%20IGBC,%20IBB%20e%20IBOMED%2f1.1.1.IMBC_COLCAP%20IQY&lang=es&NQUser=publico&NQPassword=publico&SyncOperation=1'
content_type = "text/html; charset=utf-8"
while (content_type == "text/html; charset=utf-8") {
request <- GET(url,
add_headers(`Accept` = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
`Accept-Encoding` = 'gzip, deflate',
`Accept-Language` = 'es-ES,es;q=0.9',
`Connection` = 'keep-alive',
`Host` = 'obieebr.banrep.gov.co',
`User-Agent` = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'),
write_disk("COLCAP_daily.xlsx", overwrite = T),
verbose()
)
content_type = request$all_headers[[1]]$headers$`content-type`
}
I hope the example is helpful, if you still have doubts we could review it in more detail. Regards.
Upvotes: 1
Reputation: 2289
I hope this example will guide you
library(readxl)
library(httr)
url1<-'https://evs.nci.nih.gov/ftp1/CDISC/SDTM/SDTM%20Terminology.xls'
GET(url1, write_disk(tf <- tempfile(fileext = ".xls")))
df <- read_excel(tf, 2L)
str(df)
If you can download the file you could read it as follows
library(readxl)
datos <- read_xlsx("C:/Users/USER/Downloads/1.1. Serie historica_IQY.xlsx", skip = 8, n_max = 369)
Upvotes: 5