在 R 中下载 Kaggle zip 文件 [英] Downloading Kaggle zip files in R
本文介绍了在 R 中下载 Kaggle zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图直接从我的 R 代码中的 Kaggle 空间下载 zip 文件.不幸的是,它并不正确.这是发生的事情:
I'm attempting to download zip files directly from the Kaggle space in my R code itself. Unfortunately, it's not working out right. Here's what's happening:
对于位于 https://www.kaggle.com/的旧金山犯罪数据集c/sf-犯罪/数据
取第一个数据集:test.csv.zip:https://www.kaggle.com/c/sf-crime/download/test.csv.zip
Take the first data set: test.csv.zip: https://www.kaggle.com/c/sf-crime/download/test.csv.zip
我使用的是 R 代码:
I'm using R code:
download.file(url='https://www.kaggle.com/c/sf-crime/download/test.csv.zip', destfile = 'test.zip',method = 'curl')
代替原来的 18.75MB 文件,R 只下载一个 183 字节的文件.
In place of the original 18.75MB file, R only downloads a 183byte file.
会话输出:
% 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
我做错了什么?
提前致谢,拉胡尔
推荐答案
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.
FYI : use RCurl like a web client.
这篇关于在 R 中下载 Kaggle zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文