2014-02-21 59 views
4

使用R從互聯網下載文件非常簡單,並且has been addressed previously儘管彈出式菜單通過R從互聯網下載文件

我的問題是關於如何通過似乎阻止我的下載執行的彈出消息。具體來說,

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

給我的垃圾小文件,而不是預期的18兆字節的文件,如果你去了the website,進入今年2012和季度4手動,你會得到。我懷疑問題在於,如手動執行時所看到的,彈出式窗口會中斷下載過程,詢問是保存還是打開文件。有沒有辦法自動通過彈出窗口(即通過download.file)?

回答

0
4

這可以用硒做見https://github.com/ropensci/RSelenium

require(RSelenium) 
fprof <- makeFirefoxProfile(list(browser.download.dir = "C:\\temp" 
           , browser.download.folderList = 2L 
           , browser.download.manager.showWhenStarting = FALSE 
           , browser.helperApps.neverAsk.saveToDisk = "application/zip")) 
RSelenium::startServer() 
remDr <- remoteDriver(extraCapabilities = fprof) 
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()