我需要从" http://www.elections.state.md.us"下载一些CSV文件。
这是我的代码。
url <- "http://www.elections.state.md.us/elections/2012/election_data/index.html"
# recognize the links
links <- getHTMLLinks(url)
filenames <- links[str_detect(links,"_General.csv")]
filenames_list <- as.list(filenames)
filenames
# create a function
downloadcsv <- function(filename,baseurl,folder){
dir.create(folder,showWarnings = FALSE)
fileurl <- str_c(baseurl,filename)
if(!file.exists(str_c(folder,"/",filename))){
download.file(fileurl,
destfile = str_c(folder,"/",filename))
# 1 sec delay between files
Sys.sleep(1)
}
}
library(plyr)
l_ply(filenames_list,downloadcsv,
baseurl = "www.elections.state.md.us/elections/2012/election_data/",
folder = "elec12_maryland")
错误出现为:
download.file中的错误(fileurl,destfile = str_c(文件夹,"/",, FiLENAME((:url'www.elections.state.md.us/elections/2012/election_data/state_congressional_districts_2012_general.general.csv'
中不支持方案。
但是,当我尝试将URL粘贴到IE中时,它确实有效。那么我的代码问题是什么?
任何想法都会有所帮助。
事实证明,URL必须以http://,https://,ftp://file://的方式开头。因此,在最后一行,我将代码更改为
l_ply(filenames_list,downloadcsv,
baseurl = "http://www.elections.state.md.us/elections/2012/election_data/",
folder = "elec12_maryland")
它有效。