我看到很多用 RCurl 下载二进制文件的例子是这样的:
library("RCurl")
curl = getCurlHandle()
bfile=getBinaryURL (
"http://www.example.com/bfile.zip",
curl= curl,
progressfunction = function(down, up) {print(down)}, noprogress = FALSE
)
writeBin(bfile, "bfile.zip")
rm(curl, bfile)
如果下载量很大,我想最好将它同时写入存储介质,而不是全部提取到内存中。
在 RCurl 文档中,有一些示例可以按块获取文件并在下载时对其进行操作,但它们似乎都指的是文本块。
你能举一个有效的例子吗?
更新
用户建议使用 R native
download file
与 mode = 'wb'
二进制文件的选项。在许多情况下, native 函数是一种可行的替代方案,但有许多用例(https、cookie、表单等)不适合 native 函数,这就是 RCurl 存在的原因。
最佳答案
这是工作示例:
library(RCurl)
#
f = CFILE("bfile.zip", mode="wb")
curlPerform(url = "http://www.example.com/bfile.zip", writedata = f@ref)
close(f)
它将直接下载到文件。返回的值将是(而不是下载的数据)请求的状态(0,如果没有发生错误)。
提及
CFILE
RCurl 手册上的内容有点简洁。希望将来它会包含更多详细信息/示例。为方便起见,将相同的代码打包为一个函数(并带有进度条):
bdown=function(url, file){
library('RCurl')
f = CFILE(file, mode="wb")
a = curlPerform(url = url, writedata = f@ref, noprogress=FALSE)
close(f)
return(a)
}
## ...and now just give remote and local paths
ret = bdown("http://www.example.com/bfile.zip", "path/to/bfile.zip")
关于r - 使用 R/RCurl 高效下载大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14426359/