问题描述
我正在尝试使用 R 从网站下载 PDF.
I am trying to download PDFs from a website using R.
我有一个 PDF-URL 向量 (pdfurls) 和一个目标文件名向量 (destinations):
I have a vector of the PDF-URLs (pdfurls) and a vector of destination file names (destinations):
例如:
pdfurls <- c("http://website/name1.pdf", "http://website/name2.pdf")
destinations <- c("C:/username/name1.pdf", "C:/username/name2.pdf")
我使用的代码是:
for(i in 1:length(urls)){
download.file(urls, destinations, mode="wb")}
然而,当我运行代码时,R 访问 URL,下载第一个 PDF,并一遍又一遍地重复下载同一个 PDF.
However, when I run the code, R accesses the URL, downloads the first PDF, and repeats downloading the same PDF over and over again.
我已经阅读了这篇文章:R 函数的循环并且想知道是否这与函数本身有关还是我的循环有问题?
I have read this post: for loop on R function and was wondering if this has something to do with the function itself or is there a problem with my loop?
代码类似于这里的帖子:如何在 R 中使用循环下载多个文件? 所以我想知道为什么它不起作用,以及是否有更好的方法使用 R 下载多个文件.
The code is similar to the post here: How to download multiple files using loop in R? so I was wondering why it is not working and if there is a better way to download multiple files using R.
推荐答案
我认为你的循环基本没问题,只是你忘记索引 urls
和 destinations
对象.
I think your loop is mostly fine, except you forgot to index the urls
and destinations
objects.
实际上,我建议在定义 for
循环时养成使用 seq_along
而不是 1:length()
的习惯.>
Tangentially, I would recommend getting in the habit of using seq_along
instead of 1:length()
when defining for
loops.
for(i in seq_along(urls)){
download.file(urls[i], destinations[i], mode="wb")
}
或者按照 @docendodiscimus 的建议使用 Map :
Map(function(u, d) download.file(u, d, mode="wb"), urls, destinations)
这篇关于使用“download.file"下载多个文件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!