gpt4 book ai didi

python - 使用 Python 下载 Kaggle 数据集

转载 作者:太空宇宙 更新时间:2023-11-03 12:26:29 27 4
gpt4 key购买 nike

我尝试使用 python 下载 kaggle 数据集。但是,我在使用 request 方法时遇到了问题,下载的输出 .csv 文件是损坏的 html 文件。

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)你必须做以下内容:

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 - 使用 Python 下载 Kaggle 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49386920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com