【问题标题】:Download a file from HTTPS using download.file()使用 download.file() 从 HTTPS 下载文件
【发布时间】:2014-05-26 13:31:24
【问题描述】:

我想使用download.file()将在线数据读取到R,如下所示。

URL <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv"
download.file(URL, destfile = "./data/data.csv", method="curl")

有人建议我添加行setInternet2(TRUE),但还是不行。

我得到的错误是:

Warning messages:
1: running command 'curl  "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv"  -o "./data/data.csv"' had status 127 
2: In download.file(URL, destfile = "./data/data.csv", method = "curl",  :
  download had nonzero exit status

感谢您的帮助。

【问题讨论】:

  • 您看到的问题是什么?它是否因某些错误而失败或根本不返回控制台?它是否显示不更新的进度条?额外信息将有助于诊断问题。
  • 你安装了curl吗?
  • @sgibb 嗨。我正在为 R 使用 Windows 8 和 3.0.3。它说没有包卷曲。
  • 您应该将您的标题更改为 - 从 R 中的 https 下载文件。

标签: r csv https


【解决方案1】:

尝试 RCurl 包可能是最简单的。安装软件包并尝试以下操作:

# install.packages("RCurl")
library(RCurl)
URL <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv"
x <- getURL(URL)
## Or 
## x <- getURL(URL, ssl.verifypeer = FALSE)
out <- read.csv(textConnection(x))
head(out[1:6])
#   RT SERIALNO DIVISION PUMA REGION ST
# 1  H      186        8  700      4 16
# 2  H      306        8  700      4 16
# 3  H      395        8  100      4 16
# 4  H      506        8  700      4 16
# 5  H      835        8  800      4 16
# 6  H      989        8  700      4 16
dim(out)
# [1] 6496  188

download.file("https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv",destfile="reviews.csv",method="libcurl")

【讨论】:

  • 你好阿南达。但是我在使用 getURL(URL) 时收到错误消息,如下所示 > x
  • @Yin,您可以尝试在getURL 语句中添加ssl.verifypeer = FALSE
  • @Yin,如果答案有帮助,请考虑投票。如果它解决了您的问题,请考虑接受它。
  • 这不是解决方案!状态 127 表示“找不到命令”。他/她只需要安装 CURL!
  • @Muktadir,如果你提到问题下的 cmets,你会看到我已经问过他们了。
【解决方案2】:

这是 2014 年 11 月的更新。我发现设置 method='curl' 对我有用(而 method='auto' 没有)。

例如:

# does not work
download.file(url='https://s3.amazonaws.com/tripdata/201307-citibike-tripdata.zip',
              destfile='localfile.zip')

# does not work. this appears to be the default anyway
download.file(url='https://s3.amazonaws.com/tripdata/201307-citibike-tripdata.zip',
              destfile='localfile.zip', method='auto')

# works!
download.file(url='https://s3.amazonaws.com/tripdata/201307-citibike-tripdata.zip',
              destfile='localfile.zip', method='curl')

【讨论】:

【解决方案3】:

我用下面的代码成功了:

url = "http://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv"
x = read.csv(file=url)

请注意,我已将协议从 https 更改为 http,因为 R 似乎不支持第一个协议。

【讨论】:

  • 这个“解决方案”的问题是不是所有的https url都可以用http代替。 “RCurl”包通常可以很好地处理这些情况。
  • 这不是问题的解决方案。仅当您无法解决问题时才应考虑变通方法。
  • 这解决了问题,不需要安装外部依赖项或弄乱 SSL 证书。它可能并非在所有情况下都有效,但在这种情况下有效。
【解决方案4】:

如果使用 RCurl,您会在 GetURL() 函数上收到 SSL 错误,然后在 GetURL() 之前设置这些选项。这将全局设置 CurlSSL 设置。

扩展代码:

install.packages("RCurl")
library(RCurl)
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))   
URL <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv"
x <- getURL(URL)

使用 R3.1.0 在 Windows 7 64 位上为我工作!

【讨论】:

  • 你可以通过按 Control+K 而不是使用反引号来重新格式化它吗?
  • 这是一个很好的答案!有没有办法将这些选项设置为跨不同 R 会话的默认选项?
【解决方案5】:

提供 curl 包作为我发现从在线数据库中提取大文件时可靠的替代方案。在最近的一个项目中,我不得不从在线数据库下载 120 个文件,发现它的传输时间只有一半,而且比 download.file 可靠得多。

#install.packages("curl")
library(curl)
#install.packages("RCurl")
library(RCurl)

ptm <- proc.time()
URL <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv"
x <- getURL(URL)
proc.time() - ptm
ptm

ptm1 <- proc.time()
curl_download(url =URL ,destfile="TEST.CSV",quiet=FALSE, mode="wb")
proc.time() - ptm1
ptm1

ptm2 <- proc.time()
y = download.file(URL, destfile = "./data/data.csv", method="curl")
proc.time() - ptm2
ptm2

在这种情况下,您的网址上的粗略时间显示传输时间没有一致的差异。在我的应用程序中,在脚本中使用 curl_download 从网站选择和下载 120 个文件将我的传输时间从每个文件 2000 秒减少到 1000 秒,并将可靠性从 50% 提高到 120 个文件中的 2 个故障。该脚本发布在我对之前提出的问题的回答中,请参阅 。

【讨论】:

    【解决方案6】:

    127 表示找不到命令

    在您的情况下,找不到 curl 命令。因此,这意味着没有找到 curl。

    您需要安装/重新安装 CURL。就这样。从http://curl.haxx.se/download.html获取适用于您操作系统的最新版本

    安装前关闭 RStudio。

    【讨论】:

      【解决方案7】:

      尝试使用繁重的文件

      library(data.table)
      URL <- "http://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv"
      x <- fread(URL)
      

      【讨论】:

        【解决方案8】:

        遇到与 UserR 完全相同的问题(原始问题),我也在使用 Windows 7。我尝试了所有建议的解决方案,但它们都不起作用。

        我通过以下方式解决了问题:

        1. 使用 RStudio 代替 R 控制台。

        2. 实现 R 的版本(从 3.1.0 到 3.1.1),以便库 RCurl 在其上运行正常。 (虽然我的系统是 64 位,但我现在使用的是 R3.1.1 32 位)。

        3. 我将 URL 地址键入为 https(安全连接)并使用 / 而不是反斜杠 \\

        4. 设置method = "auto"

        它现在对我有用。您应该会看到以下消息:

        Content type 'text/csv; charset=utf-8' length 9294 bytes
        opened URL
        downloaded 9294 by
        

        【讨论】:

          【解决方案9】:

          您可以设置全局选项并尝试-

          options('download.file.method'='curl')
          download.file(URL, destfile = "./data/data.csv", method="auto")
          

          有关问题,请参阅链接- https://stat.ethz.ch/pipermail/bioconductor/2011-February/037723.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-01-07
            • 1970-01-01
            • 2015-09-11
            • 1970-01-01
            • 2012-08-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多