R使用Rcurl从FTP下载多个文件 [英] R Downloading multiple file from FTP using Rcurl

查看:182
本文介绍了R使用Rcurl从FTP下载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R.的新用户。
我正在尝试从ftp服务器(我从用户名和密码获得)下载7.000个文件(.nc格式)。在网站上,每个文件都是下载链接。我想下载所有文件(.nc)。

I'm a new R. user. I am trying to download 7.000 files(.nc format) from ftp server ( which I got from user and password). On the website, each file is a link to download. I would like to download all the files (.nc).

我感谢任何可以帮助我如何在R中运行这些作业的人。这只是我尝试过的一个例子使用Rcurl和一个循环来做,并通知我:无法下载所有文件。

I thank anyone who can help me how to run those jobs in R. Just an example what I have tried to do using Rcurl and a loop and informs me: cannot download all files.

library(RCurl)

url<- "ftp://ftp.my.link.fr/1234/"
userpwd <- userpwd="user:password"
destination <- "/Users/ME/Documents"
filenames <- getURL(url, userpwd="user:password", 
ftp.use.epsv = FALSE, dirlistonly = TRUE)

for(i in seq_along(url)){
  download.file(url[i], destination[i], mode="wb")
}

我该怎么做?

推荐答案

d看到目录中的文件(即对象 filenames )将被列为一个长字符串。要获取所有文件名的对象作为字符向量,您可以尝试:

The first thing you'd see is that the files in your directory, ie the object filenames, would be listed as one long string. To obtain an object of all file names as a character vector, you may try:

    files <- unlist(strsplit(filenames, '\n'))

从这里开始,这只是循环遍历所有目录中的文件。我建议您使用curl软件包而不是Rcurl来下载文件,因为对于每个下载请求而言,更容易提供身份验证信息。

From here on, it's simply a matter of looping through all the files in the directory. I recommend you use the curl package, not Rcurl, to download the files, as it's easier to supply auth info for every download request.

    library(curl)
    h <- new_handle()
    handle_setopt(h, userpwd = "user:pwd")

,然后

    lapply(files, function(filename){
    curl_download(paste(url, filename, sep = ""), destfile = filename, handle = h)
    })

这篇关于R使用Rcurl从FTP下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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