r - 尽管有弹出窗口,但仍通过 R 从 Internet 下载文件

标签 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")

给我一个小垃圾文件,而不是你去 the website 时会得到的所需的 18 兆字节文件。并进入年份 2012和季度4手动。我怀疑问题在于,正如您手动执行时所看到的那样,一个弹出窗口会中断下载过程,询问是保存文件还是打开文件。有没有办法自动绕过弹出窗口(即,通过 download.file )?

最佳答案

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

关于r - 尽管有弹出窗口,但仍通过 R 从 Internet 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21944016/

相关文章:

html - 为什么我的文件下载链接似乎有效,但找不到该文件?

jquery 单击 href 链接 - 必须单击两次

r - 添加 UI 元素的一部分作为 introjs() 教程的步骤

r - 用线图覆盖箱线图

r - 将具有相同名称的两列粘贴到 R 中的同一数据框中

android - 弹出窗口显示在屏幕中央和上下文之外

macos - NSWindow 的标题作为指示器弹出按钮

r - 混合模型的 R 和 SAS 中的 AIC 计算不匹配

javascript - 为什么无法使用ajax请求下载文件?

node.js - 下载 csv 文件 node.js