尽管有弹出窗口,但通过R从互联网下载文件

时间:2014-02-21 19:56:57

标签: r download popup

使用R从Internet下载文件很简单,has been addressed previously

我的问题是如何通过一条似乎阻止我的下载执行的弹出消息。具体地,

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)?

2 个答案:

答案 0 :(得分:4)

这可以通过Selenium查看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()

答案 1 :(得分:0)