有没有办法告诉 R 或 RCurl 包在超过指定时间段时放弃尝试下载网页并转移到下一行代码?例如:
> library(RCurl)
> u = "http://photos.prnewswire.com/prnh/20110713/NY34814-b"
> getURL(u, followLocation = TRUE)
> print("next line") # programme does not get this far
这只会挂在我的系统上,而不是进入最后一行。
编辑:根据@Richie Cotton 下面的回答,虽然我可以“有点”实现我想要的,但我不明白为什么它需要比预期更长的时间。例如,如果我执行以下操作,系统会挂起,直到我在 RGUI 中选择/取消选择“Misc >> Buffered Output”选项:
> system.time(getURL(u, followLocation = TRUE, .opts = list(timeout = 1)))
Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) :
Operation timed out after 1000 milliseconds with 0 out of 0 bytes received
Timing stopped at: 0.02 0.08 ***6.76***
解决方案:基于@Duncan 下面的帖子,然后查看 curl 文档,我通过使用 maxredirs 选项找到了解决方案,如下所示:
> getURL(u, followLocation = TRUE, .opts = list(timeout = 1, maxredirs = 2, verbose = TRUE))
非常感谢你,
托尼·布雷亚尔
O/S: Windows 7
R version 2.13.0 (2011-04-13) Platform: x86_64-pc-mingw32/x64 (64-bit)
attached base packages:
[1] stats graphics grDevices utils
datasets methods base
other attached packages:
[1] RCurl_1.6-4.1 bitops_1.0-4.1
loaded via a namespace (and not attached):
[1] tools_2.13.0