使用download.file()从HTTPS下载文件

use*_*seR 30 csv https r

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

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

有人向我建议我添加该行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
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助.

A5C*_*2T1 41

尝试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")
Run Code Online (Sandbox Code Playgroud)

  • @Yin,你可以尝试将`ssl.verifypeer = FALSE`添加到`getURL`语句中. (4认同)

arv*_*000 12

这是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')
Run Code Online (Sandbox Code Playgroud)


Bau*_*ann 5

我已经成功使用以下代码:

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

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

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

小智 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
Run Code Online (Sandbox Code Playgroud)

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