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

k4emjkb1  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(124)

使用R从互联网下载文件很容易,而且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)?

ctehm74n

ctehm74n1#

这可以通过Selenium来实现,参见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()

字符串

相关问题