【问题标题】:Downloading Kaggle zip files in R在 R 中下载 Kaggle zip 文件
【发布时间】:2016-02-09 22:52:04
【问题描述】:

我正在尝试直接从我的 R 代码本身的 Kaggle 空间下载 zip 文件。不幸的是,它的工作不正确。以下是正在发生的事情:

旧金山犯罪数据集https://www.kaggle.com/c/sf-crime/data

取第一个数据集:test.csv.zip: https://www.kaggle.com/c/sf-crime/download/test.csv.zip

我正在使用 R 代码:

download.file(url='https://www.kaggle.com/c/sf-crime/download/test.csv.zip', destfile = 'test.zip',method = 'curl')

代替原来的 18.75MB 文件,R 只下载一个 183 字节的文件。

会话输出:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0100   183  100   183    0     0    665      0 --:--:-- --:--:-- --:--:--   667

我做错了什么?

提前致谢, 拉胡尔

【问题讨论】:

  • 你登录 Kaggle 了吗?
  • 你可能刚刚收到这个:<html><head><title>Object moved</title></head><body> <h2>Object moved to <a href="/account/login?ReturnUrl=%2fc%2fsf-crime%2fdownload%2ftest.csv.zip">here</a>.</h2> </body></html>
  • 是的,我已经登录了 Kaggle。

标签: r zip kaggle


【解决方案1】:
library(RCurl)

#Set your browsing links 
loginurl = "https://www.kaggle.com/account/login"
dataurl  = "https://www.kaggle.com/c/titanic/download/train.csv"

#Set user account data and agent
pars=list(
  UserName="suiwenfeng@live.cn",
  Password="-----"
)
agent="Mozilla/5.0" #or whatever 

#Set RCurl pars
curl = getCurlHandle()
curlSetOpt(cookiejar="cookies.txt",  useragent = agent, followlocation = TRUE, curl=curl)
#Also if you do not need to read the cookies. 
#curlSetOpt(  cookiejar="", useragent = agent, followlocation = TRUE, curl=curl)

#Post login form
welcome=postForm(loginurl, .params = pars, curl=curl)

bdown=function(url, file, curl){
  f = CFILE(file, mode="wb")
  curlPerform(url = url, writedata = f@ref, noprogress=FALSE, curl = curl)
  close(f)
}

ret = bdown(dataurl, "c:\\test.csv",curl)

rm(curl)
gc()

仅供参考:像 Web 客户端一样使用 RCurl。

【讨论】:

    猜你喜欢
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    相关资源
    最近更新 更多