1
在下面的代码中,每次尝试下载PDF时,handle
部分似乎都会给我一个错误。在R中下载PDF所需的帮助
url <- "http://brocktonpolice.com/wp-content/uploads/"
filename <- paste0(format(AllDays, '%Y/%m/%m%d%Y'), '.pdf')
filenames_list <- str_extract_all(filenames, 'uploads.+pdf')
downloadPDF <- function(filename, baseurl, folder, handle){
dir.create(folder, showWarnings = FALSE)
fileurl <- str_c(baseurl, filename)
if (!file.exists(str_c(folder,"/",filename))) {
content <- getBinaryURL(fileurl, curl = handle)
writeBin(content, str_c(folder,"/",filename))
Sys.sleep(1)
}
}
handle <- getCurlHandle(useragent = str_c(R.version$platform,
R.version.string, sep = ", "),
httpheader = c(from = "[email protected]"))
l_ply(filenames_list, downloadPDF,
baseurl = "http://brocktonpolice.com/wp-content/uploads/",
folder = "Police_logs")
我对如何下载这些PDF的想法已经用完了。 以下是我如何生成指向所有PDF的链接。
prefix <- "http://brocktonpolice.com/wp-content/uploads/"
AllDays <- seq.Date(from = as.Date('2015-01-01'), to = Sys.Date(), by = "day")
links <- paste0(prefix, format(AllDays, '%Y/%m/%m%d%Y'), '.pdf')
print(links)
ps:如果您可以想出其他方式下载PDF文件,请分享您的代码。
注意某些URL可能会给出一个错误,因为有时日和月不加零的时候都小于10