我正在尝试直接从我的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
我做错了什么?
提前致谢, 拉胡
答案 0 :(得分:2)
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。