在下面的代码中,handle
每次我尝试下载 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 = "jomisilfe@gmail.com"))
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 时没有前导零。