Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file from internet via R despite the popup

Tags:

r

download

popup

Downloading a file from the internet using R is easy and has been addressed previously.

My question regards how to get past a popup message that seems to prevent my download from executing. Specifically,

download.file(url = "https://www.chicagofed.org/applications/bhc_data/bhcdata_index.cfm?DYR=2012&DQIR=4", destfile = "data/test.zip")

gives me a little file of garbage instead of the desired 18 megabyte file that you would get if you went to the website and entered the year 2012 and the quarter 4 manually. I suspect that the issue is that, as can be seen when you do it manually, a popup window interrupts the download process, asking whether to save the file or open it. Is there any way to get past the popup automatically (i.e., via download.file)?

like image 858
zkurtz Avatar asked Feb 21 '14 19:02

zkurtz


People also ask

How do I trigger a download in HTML?

To trigger a file download on a button click we will use a custom function or HTML 5 download attribute. The download attribute simply uses an anchor tag to prepare the location of the file that needs to be downloaded.

How does a browser download a file?

Traditionally, the file to be downloaded is first requested from a server through a client — such as a user's web browser. The server then returns a response containing the content of the file and some instructional headers specifying how the client should download the file.

How does HTTP file download work?

When a user clicks a download link and tries to download files, thebrowser sends an HTTP GET request to the web server. At the sametime the file name to be downloaded and the file location are alsosent with the HTTP GET request.


1 Answers

This can be done with Selenium see https://github.com/ropensci/RSelenium.

require(wdman)
require(RSelenium)


selPort <- 4444L
fprof <- makeFirefoxProfile(list(browser.download.dir = "C:\\temp"
                                 ,  browser.download.folderList = 2L
                                 , browser.download.manager.showWhenStarting = FALSE
                                 , browser.helperApps.neverAsk.saveToDisk = "application/zip"))
selServ <- selenium(port = selPort)
remDr <- remoteDriver(extraCapabilities = fprof, port = selPort)
remDr$open(silent = TRUE)
remDr$navigate("https://www.chicagofed.org/applications/bhc_data/bhcdata_index.cfm")
# click year 2012
webElem <- remDr$findElement("name", "SelectedYear")
webElems <- webElem$findChildElements("css selector", "option")
webElems[[which(sapply(webElems, function(x){x$getElementText()}) == "2012" )]]$clickElement()

# click required quarter

webElem <- remDr$findElement("name", "SelectedQuarter")
Sys.sleep(1)
webElems <- webElem$findChildElements("css selector", "option")
webElems[[which(sapply(webElems, function(x){x$getElementText()}) == "4th Quarter" )]]$clickElement()

# click button

webElem <- remDr$findElement("id", "downloadDataFile")
webElem$clickElement()
like image 61
jdharrison Avatar answered Sep 25 '22 15:09

jdharrison