R 下载,文件冻结

R 下载,文件冻结,r,download,freeze,R,Download,Freeze,我想从网站上下载一些图片。我有一系列的url图片需要下载。因此,我使用以下代码运行它: dlphoto <- function(x){ print(x) setTimeLimit(5) Sys.sleep(0.3) download.file(x , destfile = basename(x)) } 然后我换成了这个: dlphoto(allimage[c(1:50),2]) dlphoto(allimage[c(51:100),2]) dlpho

我想从网站上下载一些图片。我有一系列的
url
图片需要下载。因此,我使用以下代码运行它:

 dlphoto <- function(x){
   print(x)
   setTimeLimit(5)
   Sys.sleep(0.3)
   download.file(x , destfile = basename(x))
   }
然后我换成了这个:

 dlphoto(allimage[c(1:50),2])
 dlphoto(allimage[c(51:100),2])
 dlphoto(allimage[c(101:150),2])
 dlphoto(allimage[c(151:200),2])
 and so on untill 15000
等等。但它仍然冻结了很多。每次它死了,我必须关闭R,搜索进程到达的地方,然后从那里开始。我经常收到这样的警告信息:

   Error in download.file(x, destfile = basename(x)) : 
   reached CPU time limit
还有,你能帮我把下载的照片保存在

    /Users/name/Desktop/M2/Mémoire M2/Scrapingtest/photos

非常感谢

有两个可能的改进。我假设
OP
正在使用
base
中的
download.file
包,如果未设置
method
quiet=T
,则该包一次仅支持单个文件

因此,修复方法应该是在
download.file
函数中使用
method=“libcurl”
quiet=TRUE
。更改后的功能:

dlphoto <- function(x){
    print(x)
    download.file(x , destfile = basename(x), method="libcurl", quiet = TRUE)
}
注意:在上述两种情况下,进度条都不会显示

我认为
options
中的
timeout
值足够好,可以确保在出现延迟时从
download.file
返回

应检查
download.file
返回值的错误。任何
非零
返回值表示故障

若你们想看到进度条(一次处理1500个文件可能不需要进度条),那个么这个函数应该修改为一次处理1个文件。修改后的功能将是:

# This function will display progressbar for each file
dlphoto <- function(x){
  for(file in x){
    print(fine)
    download.file(file , destfile = basename(file))
  }
}
#此函数将为每个文件显示progressbar

dlphoto如何调用函数
dlphoto
下载15000个URL?请同时添加该代码。谢谢提醒。只是增加了问题的精确度。通过删除函数中无用的
setTimeLimit
调用,您可以很容易地防止错误消息。另外,删除
Sys.sleep
:它同样没有用。
download.file(x , destfile = basename(x), method="libcurl", quiet = TRUE)
# This function will display progressbar for each file
dlphoto <- function(x){
  for(file in x){
    print(fine)
    download.file(file , destfile = basename(file))
  }
}