我想从网站下载多个以 .txt 结尾的文本文件。我正在尝试使用这些答案 Download All Files From a Folder on a Website
因此,我更改了 txt 结尾文件的答案,但此页面中的所有解决方案均不适合我。我想我必须改变第一个答案的这一行
xpQuery = "//a/@href['.txt'=substring(., string-length(.) - 3)]"
对于 txt 文件,但我不知道如何。
编辑后的代码如下:
## your base url
url <- "https://..."
## query the url to get all the file names ending in '.txt'
txts <- XML::getHTMLLinks(
url,
xpQuery = "//a/@href['.txt'=substring(., string-length(.) - 3)]"
)
## create a new directory 'mytxts' to hold the downloads
dir.create("mytxts")
## save the current directory path for later
wd <- getwd()
## change working directory for the download
setwd("mytxts")
## create all the new files
file.create(txts)
## download them all
lapply(paste0(url, txts), function(x) download.file(x, basename(x)))
## reset working directory to original
setwd(wd)