在R中批量下载压缩文件

在R中批量下载压缩文件,r,batch-file,R,Batch File,我正在尝试从网站下载压缩文件,如。 既然有很多文件,有没有办法批量下载而不是逐个下载?理想情况下,下载的文件可以在下载后批量解压缩。 我尝试使用systemcurl等。。但是得到了很多错误和警告 有什么想法或建议吗 谢谢 它不是R,但您可以轻松使用该程序,忽略robots.txt: wget-r-no parent-e robots=off-accept*.gz 要下载该目录下的所有内容,可以执行以下操作: wget -r -e robots=off http://cdo.ncdc.noaa.

我正在尝试从网站下载压缩文件,如。 既然有很多文件,有没有办法批量下载而不是逐个下载?理想情况下,下载的文件可以在下载后批量解压缩。 我尝试使用systemcurl等。。但是得到了很多错误和警告

有什么想法或建议吗


谢谢

它不是R,但您可以轻松使用该程序,忽略robots.txt:

wget-r-no parent-e robots=off-accept*.gz


要下载该目录下的所有内容,可以执行以下操作:

wget -r -e robots=off http://cdo.ncdc.noaa.gov/qclcd_ascii/
这应该行得通

library(XML)
url<-c("http://cdo.ncdc.noaa.gov/qclcd_ascii/")
doc<-htmlParse(url)
#get <a> nodes.
Anodes<-getNodeSet(doc,"//a")
#get the ones with .zip's and .gz's
files<-grep("*.gz|*.zip",sapply(Anodes, function(Anode) xmlGetAttr(Anode,"href")),value=TRUE)
#make the full url
urls<-paste(url,files,sep="")
#Download each file.
mapply(function(x,y) download.file(x,y),urls,files)
以下是我对它的看法:

### Load XML package, for 'htmlParse'
require(XML)

### Read in HTML contents, extract file names.
root <- 'http://cdo.ncdc.noaa.gov/qclcd_ascii/'
doc  <- htmlParse(root)
fnames <- xpathSApply(doc, '//a[@href]', xmlValue)

### Keep only zip files, and create url paths to scrape.
fnames <- grep('zip$', fnames, value = T)
paths  <- paste0(root, fnames)

您需要服务器端访问才能将所有内容放入批处理文件中。也许你可以使用R中提供的一个并行框架下载?我们可以投票关闭这个吗?是的,我想可以!谢谢你能在我的答案上打勾吗?是的。是的。我以前不能这样做,因为我的声誉分数不够高!再次感谢您出色的解决方案!这会写入工作目录,您可以使用setwd设置该目录。要解压它们,lapplygrep.zip,files,value=true,解压将适用于zip文件,lapplygrep.tar.gz,files,value=true,untar将适用于tar.gz文件,尽管您可以直接读取.table tar.gz文件。非常感谢!这真的非常有效!
### Download data in serial, saving to the current working directory.
mapply(download.file, url = paths, destfile = fnames)

### Download data in parallel, also saving to current working directory.
require(parallel)
cl <- makeCluster(detectCores())
clusterMap(cl, download.file, url = paths, destfile = fnames, 
           .scheduling = 'dynamic')
fnames <- grep('(zip)|(gz)$', fnames, value = T)