尽管弹出窗口,仍然通过R从Internet下载文件 [英] Download file from internet via R despite the popup

查看:130
本文介绍了尽管弹出窗口,仍然通过R从Internet下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R从互联网下载文件很容易,已经我的问题是关于如何通过一个似乎阻止我的下载执行的弹出消息。具体来说,

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

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

解决方案

这可以用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(sape(webElems,function(x){x $ getElementText()})==2012)]] $ clickElement )

#点击需要季度

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

#点击按钮

webElem< - remDr $ findElement(id,downloadDataFile)
webElem $ clickElement()


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

解决方案

This can be done with Selenium see 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()

这篇关于尽管弹出窗口,仍然通过R从Internet下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆