R 无法从网络下载文件
我可以在浏览器中从此网站下载一个文件 https://www.cmegroup.com/ftpp/ftp/ftp/pub/settle/comex_future。 CSV
但是,当我尝试以下时,
url <- "https://www.cmegroup.com/ftp/pub/settle/comex_future.csv"
dest <- "C:\\COMEXfut.csv"
download.file(url, dest)
以下内容,也会收到以下错误消息
Error in download.file(url, dest) :
cannot open URL 'https://www.cmegroup.com/ftp/pub/settle/comex_future.csv'
In addition: Warning message:
In download.file(url, dest) :
InternetOpenUrl failed: 'The operation timed out'
即使选择
options(timeout = max(600, getOption("timeout")))
:有任何想法会发生这种情况吗?谢谢 !
I can download in the browser a file from this website
https://www.cmegroup.com/ftp/pub/settle/comex_future.csv
However when I try the following
url <- "https://www.cmegroup.com/ftp/pub/settle/comex_future.csv"
dest <- "C:\\COMEXfut.csv"
download.file(url, dest)
I get the following error message
Error in download.file(url, dest) :
cannot open URL 'https://www.cmegroup.com/ftp/pub/settle/comex_future.csv'
In addition: Warning message:
In download.file(url, dest) :
InternetOpenUrl failed: 'The operation timed out'
even if I choose:
options(timeout = max(600, getOption("timeout")))
any idea why is this happening ? thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

这里的问题在于,您下载的网站需要其他几个标题。提供它们的最简单方法是使用
httr
软件包,该软件包应在不到一秒钟内下载。
如果您想保存文件可以执行
,或者您只想将数据直接读取为R,甚至不保存它,则可以这样做:
The problem here is that the site from which you are downloading needs a couple of additional headers. The easiest way to supply them is using the
httr
packageThis should download in less than a second.
If you want to save the file you can do
Or if you just want to read the data straight into R without even saving it, you can do: