在R中使用download.file时如何忽略404错误

时间:2019-02-23 12:12:18

标签: r error-handling try-catch rvest

我想使用download.file()

从Internet下载一些html页面到本地

这是我的代码:

for (i in 1:length(urlist)){
loc = urlist[i]
print(loc)
des = paste("E:/my docs/urls/", i,".html", sep = "")
download.file(loc, destfile = des)
Sys.sleep(runif(1,2,5))}

这些代码有效,但是遇到一些错误时它将停止运行。

trying URL 'http://www.zhihu.com/people/1.22198E+12'
Error in download.file(loc, destfile = des) : 
cannot open URL 'http://www.zhihu.com/people/1.22198E+12'
In addition: Warning message:
In download.file(loc, destfile = des) :
cannot open URL 'http://www.zhihu.com/people/1.22198E+12': HTTP status was 
'404 Not Found'

我知道我的urlist中有一些错误的url,但是我不想检查,因为数据太大而无法检查。因此,我想知道是否有某种方法可以忽略该错误并继续下载。

0 个答案:

没有答案