本文介绍了使用Python下载Kaggle数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用python下载 kaggle数据集
。但是,使用 request
方法遇到问题,下载的输出.csv文件是损坏的html文件。
I have trying to download the kaggle dataset
by using python. However i was facing issues by using the request
method and the downloaded output .csv files is a corrupted html files.
import requests
# The direct link to the Kaggle data set
data_url = 'https://www.kaggle.com/crawford/gene-expression/downloads/actual.csv'
# The local path where the data set is saved.
local_filename = "actsual.csv"
# Kaggle Username and Password
kaggle_info = {'UserName': "myUsername", 'Password': "myPassword"}
# Attempts to download the CSV file. Gets rejected because we are not logged in.
r = requests.get(data_url)
# Login to Kaggle and retrieve the data.
r = requests.post(r.url, data = kaggle_info)
# Writes the data to a local file one chunk at a time.
f = open(local_filename, 'wb')
for chunk in r.iter_content(chunk_size = 512 * 1024): # Reads 512KB at a time into memory
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.close()
输出文件
<!DOCTYPE html>
<html>
<head>
<title>Gene expression dataset (Golub et al.) | Kaggle</title>
<meta charset="utf-8" />
<meta name="robots" content="index, follow"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta name="theme-color" content="#008ABC" />
<link rel="dns-prefetch" href="https://www.google-analytics.com" /><link rel="dns-prefetch" href="https://stats.g.doubleclick.net" /><link rel="dns-prefetch" href="https://js.intercomcdn.com" /><link rel="preload" href="https://az416426.vo.msecnd.net/scripts/a/ai.0.js" as=script /><link rel="dns-prefetch" href="https://kaggle2.blob.core.windows.net" />
<link href="/content/v/d420a040e581/kaggle/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link rel="manifest" href="/static/json/manifest.json">
<link href="//fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic" rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="/static/assets/vendor.css?v=72f4ef2ebe4f"/>
<link rel="stylesheet" type="text/css" href="/static/assets/app.css?v=d997fa977b65"/>
<script>
(function () {
var originalError = window.onerror;
window.onerror = function (message, url, lineNumber, columnNumber, error) {
var handled = originalError && originalError(message, url, lineNumber, columnNumber, error);
var blockedByCors = message && message.toLowerCase().indexOf("script error") >= 0;
return handled || blockedByCors;
};
})();
</script>
<script>
var appInsights=window.appInsights||function(config){
function i(config){t[config]=function(){var i=arguments;t.queue.push(function(){t[config].apply(t,i)})}}var t={config:config},u=document,e=window,o="script",s="AuthenticatedUserContext",h="start",c="stop",l="Track",a=l+"Event",v=l+"Page",y=u.createElement(o),r,f;y.src=config.url||"https://az416426.vo.msecnd.net/scripts/a/ai.0.js";u.getElementsByTagName(o)[0].parentNode.appendChild(y);try{t.cookie=u.cookie}catch(p){}for(t.queue=[],t.version="1.0",r=["Event","Exception","Metric","PageView","Trace","Dependency"];r.length;)i("track"+r.pop());return i("set"+s),i("clear"+s),i(h+a),i(c+a),i(h+v),i(c+v),i("flush"),config.disableExceptionTracking||(r="onerror",i("_"+r),f=e[r],e[r]=function(config,i,u,e,o){var s=f&&f(config,i,u,e,o);return s!==!0&&t["_"+r](config,i,u,e,o),s}),t
}({
instrumentationKey:"5b3d6014-f021-4304-8366-3cf961d5b90f",
disableAjaxTracking: true
});
window.appInsights=appInsights;
appInsights.trackPageView();
</script>
推荐答案
基本上,如果要使用Kaggle python API(@ minh-triet提供的解决方案是针对python的命令行非),您必须执行以下操作:
Basically, if you want to use the Kaggle python API (the solution provided by @minh-triet is for the command line not for python) you have to do the following:
import kaggle
kaggle.api.authenticate()
kaggle.api.dataset_download_files('The_name_of_the_dataset', path='the_path_you_want_to_download_the_files_to', unzip=True)
我希望这会有所帮助。
这篇关于使用Python下载Kaggle数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!