c***z 发帖数: 6348 | 1 R怎么在网址下载整个folder?
比如说这个:
ftp://ftp.bls.gov/pub/special.requests/cew/2011/
我知道的是download.file(),可是只能一个文件一个文件的下
谢各位大牛! | r**s 发帖数: 17 | 2 不能去命令行吗
【在 c***z 的大作中提到】 : R怎么在网址下载整个folder? : 比如说这个: : ftp://ftp.bls.gov/pub/special.requests/cew/2011/ : 我知道的是download.file(),可是只能一个文件一个文件的下 : 谢各位大牛!
| c***z 发帖数: 6348 | 3 能详解一下么
我倒是找到了一个法子
首先列出目录下的文件和子目录,文件直接下载,子目录调用本函数(递归)
但是下载下来的文件大小不对,大侠能帮忙看看么
library("RCurl")
# ==========================================================================
====
# Function that downloads files from URL
# ==========================================================================
====
fdownload <- function(sourcelink) {
# sourcelink <- ftp.root # test, root level
# sourcelink <- dirs[1] # test, second level
targetlink <- paste(dropbox.root, substr(sourcelink, nchar(ftp.root)+1,
nchar(sourcelink)), sep = '')
# list of contents
filenames <- getURL(sourcelink, ftp.use.epsv = FALSE, dirlistonly = TRUE)
filenames <- strsplit(filenames, "\n")
filenames <- unlist(filenames)
files <- filenames[grep('\\.', filenames)]
dirs <- setdiff(filenames, files)
if (length(dirs) != 0) {
dirs <- paste(sourcelink, dirs, '/', sep = '')
}
# files
curl <- getCurlHandle()
for (filename in files) {
# filename <- files[1]
targetfile <- paste(targetlink, filename, sep = '')
filename <- paste(sourcelink, filename, sep = '')
content <- getURLContent(filename, curl = curl)
attributes(content) <- NULL
error <- writeBin(content, targetfile)
cat(error, '\n')
}
# subfolders
for (dirname in dirs) {
# dirname <- dirs[1]
fdownload(dirname)
}
} | c***z 发帖数: 6348 | 4 ftp.root是要下载的ftp
dropbox.root是存放文件的地址
==
==
【在 c***z 的大作中提到】 : 能详解一下么 : 我倒是找到了一个法子 : 首先列出目录下的文件和子目录,文件直接下载,子目录调用本函数(递归) : 但是下载下来的文件大小不对,大侠能帮忙看看么 : library("RCurl") : # ========================================================================== : ==== : # Function that downloads files from URL : # ========================================================================== : ====
| c***z 发帖数: 6348 | 5 I got a working version now:
#=====================================================================
# Function that downloads files from URL
#=====================================================================
fdownload <- function(sourcelink) {
# sourcelink <- ftp.root # test, root level
# sourcelink <- dirs[1] # test, second level
targetlink <- paste(dropbox.root, substr(sourcelink, nchar(ftp.root)+1,
nchar(sourcelink)), sep = '')
# list of contents
filenames <- getURL(sourcelink, ftp.use.epsv = FALSE, dirlistonly = TRUE)
filenames <- strsplit(filenames, "\n")
filenames <- unlist(filenames)
files <- filenames[grep('\\.', filenames)]
dirs <- setdiff(filenames, files)
if (length(dirs) != 0) {
dirs <- paste(sourcelink, dirs, '/', sep = '')
}
# files
for (filename in files) {
# filename <- files[1]
sourcefile <- paste(sourcelink, filename, sep = '')
targetfile <- paste(targetlink, filename, sep = '')
download.file(sourcefile, targetfile)
}
# subfolders
for (dirname in dirs) {
# dirname <- dirs[1]
fdownload(dirname)
}
} |
|