在R中下载Kaggle压缩文件

3
我正在尝试在我的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

我做错了什么?

提前感谢, Rahul


2
你登录Kaggle了吗? - PereG
你可能只是收到了这个:```<html><head><title>对象已移动</title></head><body><h2>对象已移动到<a href="/account/login?ReturnUrl=%2fc%2fsf-crime%2fdownload%2ftest.csv.zip">此处</a>。</h2> </body></html>``` - Josh W.
是的,我已经登录 Kaggle。 - Rahul
1个回答

5
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()

注意:使用RCurl就像使用Web客户端一样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接