我能够使用PIP命令下载Kaggle,能够将kaggle.json文件放置到相应的文件夹中,也能够看到其中的比赛。但是当我尝试下载数据文件时,会显示一个错误。
代码如下:
import kagglefrom kaggle.api.kaggle_api_extended import KaggleApiapi = KaggleApi()api.authenticate()lis1 = api.competitions_list(search='LANL-Earthquake-Prediction')api.competition_download_files('LANL-Earthquake-Prediction')
错误信息:显示以下错误信息。
Traceback (most recent call last): File "C:\Users\rpremala003\PycharmProjects\MachineLearning\Importing datasets\Importing Kaggle through API.py", line 11, in <module> api.competition_download_files('LANL-Earthquake-Prediction') File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\api\kaggle_api_extended.py", line 718, in competition_download_files self.competitions_data_download_files_with_http_info( File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\api\kaggle_api.py", line 400, in competitions_data_download_files_with_http_info return self.api_client.call_api( File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\api_client.py", line 329, in call_api return self.__call_api(resource_path, method, File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\api_client.py", line 161, in __call_api response_data = self.request( File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\api_client.py", line 351, in request return self.rest_client.GET(url, File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\rest.py", line 247, in GET return self.request("GET", url, File "C:\Users\rpremala003\PycharmProjects\MachineLearning\venv\lib\site-packages\kaggle\rest.py", line 241, in request raise ApiException(http_resp=r)kaggle.rest.ApiException: (403)Reason: ForbiddenHTTP response headers: HTTPHeaderDict({'Date': 'Sun, 04 Jul 2021 19:34:22 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Set-Cookie': 'ka_sessionid=232614eb67b588938ac922774220f567; max-age=2626560; path=/, GCLB=CPDc0e-esJK-ZQ; path=/; HttpOnly', 'Vary': 'Accept-Encoding', 'Access-Control-Allow-Credentials': 'true', 'Turbolinks-Location': 'https://www.kaggle.com/api/v1/competitions/data/download-all/LANL-Earthquake-Prediction', 'X-Kaggle-MillisecondsElapsed': '91', 'X-Kaggle-RequestId': 'd485fdb0e4674834b5c1b444fd9885de', 'X-Kaggle-ApiVersion': '1.5.12', 'X-Frame-Options': 'SAMEORIGIN', 'Strict-Transport-Security': 'max-age=63072000; includeSubDomains; preload', 'Content-Security-Policy': "object-src 'none'; script-src 'nonce-q8ly/A24jHgL5gZmidMI6A==' 'report-sample' 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:; frame-src 'self' https://www.kaggleusercontent.com https://www.youtube.com/embed/ https://polygraph-cool.github.io https://www.google.com/recaptcha/ https://form.jotform.com https://submit.jotform.us https://submit.jotformpro.com https://submit.jotform.com https://www.docdroid.com https://www.docdroid.net https://kaggle-static.storage.googleapis.com https://kaggle-static-staging.storage.googleapis.com https://kkb-dev.jupyter-proxy.kaggle.net https://kkb-staging.jupyter-proxy.kaggle.net https://kkb-production.jupyter-proxy.kaggle.net https://kkb-production.firebaseapp.com https://apis.google.com https://content-sheets.googleapis.com/ https://accounts.google.com/ https://storage.googleapis.com https://docs.google.com; base-uri 'none'; report-uri https://csp.withgoogle.com/csp/kaggle/20201130;", 'X-Content-Type-Options': 'nosniff', 'Referrer-Policy': 'strict-origin-when-cross-origin', 'Via': '1.1 google', 'Alt-Svc': 'clear'})HTTP response body: b'{"code":403,"message":"You must accept this competition\\u0027s rules before you\\u0027ll be able to download files."}'
回答:
返回的错误信息描述了问题的根源:
HTTP response body: b'{"code":403,"message":"You must accept this competition\\u0027s rules before you\\u0027ll be able to download files."}'
根据Kaggle API的文档:
就像通过用户界面正常参与比赛一样,您必须阅读并接受规则才能下载数据或提交内容。您无法通过API接受比赛规则。您必须访问Kaggle网站并在那里接受规则。
如果您已经接受了比赛的规则,可能您没有使用对应用户的令牌进行身份验证。