问题描述
我正在尝试使用R从网站下载PDF.
I am trying to download PDFs from a website using R.
我有一个PDF-URL的向量( pdfurls )和一个目标文件名的向量(目的地):
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.
我已经阅读了这篇文章: for 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 的建议使用地图:
Or using Map as suggested by @docendodiscimus :
Map(function(u, d) download.file(u, d, mode="wb"), urls, destinations)
这篇关于使用"download.file"下载多个文件.功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!