我正在从这个页面下载 xls 文件,并使用 R 循环遍历 url(基于第一步):
getURLFilename <- function(url){
require(stringi)
hdr <-paste(curlGetHeaders(url),collapse = '')
fname <- as.vector(stri_match(hdr,regex = '(?<=filename=\\").*(?=\\")'))
fname
}
for(i in 8:56) {
i1 <- sprintf('%02d', i)
url <- paste0("https://journals.openedition.org/acrh/29", i1, "?file=1")
file <- paste0("myExcel_", i, ".xls")
if (!file.exists(file)) download.file(url, file)
}
文件已下载但已损坏。
您应该使用mode="wb"
indownload.file
以二进制模式写入文件。
library(readxl)
for (i in 8:55) {
i1 <- sprintf('%02d', i)
url <- paste0("https://journals.openedition.org/acrh/29", i1, "?file=1")
if (is.na(format_from_signature(url))) {
file <- paste0("myPdf_", i, ".pdf")
} else {
file <- paste0("myExcel_", i, ".xls")
}
if (!file.exists(file)) download.file(url, file, mode="wb")
}
现在下载的 Excel 文件没有损坏。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句