将Kaggle数据集导入Jupyter Notebook

时间:2018-10-06 16:41:59

标签: python kaggle

我正在尝试将一些数据从kaggle导入到笔记本中。我收到的错误是未经授权的401,但是我已经接受了比赛规则,并且能够下载数据。

这是我正在运行的代码:

from kaggle.api.kaggle_api_extended import KaggleApi

api = KaggleApi()
files = api.competition_download_files("twosigmanews")
api.competitions_submit("submission.csv", "my submission message", "twosigmanews")

编辑:添加了更多错误:无论我要导入哪个kaggle数据,我都会遇到相同的错误。


ApiException                              Traceback (most recent call last)
<ipython-input-7-65a92f19da82> in <module>()
      2 
      3 api = KaggleApi()
----> 4 files = api.competition_download_files("twosigmanews")
      5 api.competitions_submit("submission.csv", "my submission message", "twosigmanews")

~\Anaconda3\lib\site-packages\kaggle\api\kaggle_api_extended.py in competition_download_files(self, competition, path, force, quiet)
    637             quiet: suppress verbose output (default is False)
    638         """
--> 639         files = self.competition_list_files(competition)
    640         if not files:
    641             print('This competition does not have any available data files')

~\Anaconda3\lib\site-packages\kaggle\api\kaggle_api_extended.py in competition_list_files(self, competition)
    554         """
    555         competition_list_files_result = self.process_response(
--> 556             self.competitions_data_list_files_with_http_info(id=competition))
    557         return [File(f) for f in competition_list_files_result]
    558 

~\Anaconda3\lib\site-packages\kaggle\api\kaggle_api.py in competitions_data_list_files_with_http_info(self, id, **kwargs)
    416             _preload_content=params.get('_preload_content', True),
    417             _request_timeout=params.get('_request_timeout'),
--> 418             collection_formats=collection_formats)
    419 
    420     def competitions_list(self, **kwargs):  # noqa: E501

~\Anaconda3\lib\site-packages\kaggle\api_client.py in call_api(self, resource_path, method, path_params, query_params, header_params, body, post_params, files, response_type, auth_settings, async_req, _return_http_data_only, collection_formats, _preload_content, _request_timeout)
    332                                    response_type, auth_settings,
    333                                    _return_http_data_only, collection_formats,
--> 334                                    _preload_content, _request_timeout)
    335         else:
    336             thread = self.pool.apply_async(self.__call_api, (resource_path,

~\Anaconda3\lib\site-packages\kaggle\api_client.py in __call_api(self, resource_path, method, path_params, query_params, header_params, body, post_params, files, response_type, auth_settings, _return_http_data_only, collection_formats, _preload_content, _request_timeout)
    163             post_params=post_params, body=body,
    164             _preload_content=_preload_content,
--> 165             _request_timeout=_request_timeout)
    166 
    167         self.last_response = response_data

~\Anaconda3\lib\site-packages\kaggle\api_client.py in request(self, method, url, query_params, headers, post_params, body, _preload_content, _request_timeout)
    353                                         _preload_content=_preload_content,
    354                                         _request_timeout=_request_timeout,
--> 355                                         headers=headers)
    356         elif method == "HEAD":
    357             return self.rest_client.HEAD(url,

~\Anaconda3\lib\site-packages\kaggle\rest.py in GET(self, url, headers, query_params, _preload_content, _request_timeout)
    249                             _preload_content=_preload_content,
    250                             _request_timeout=_request_timeout,
--> 251                             query_params=query_params)
    252 
    253     def HEAD(self, url, headers=None, query_params=None, _preload_content=True,

~\Anaconda3\lib\site-packages\kaggle\rest.py in request(self, method, url, query_params, headers, body, post_params, _preload_content, _request_timeout)
    239 
    240         if not 200 <= r.status <= 299:
--> 241             raise ApiException(http_resp=r)
    242 
    243         return r

ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Cache-Control': 'private', 'Content-Length': '37', 'Content-Type': 'application/json; charset=utf-8', 'X-MiniProfiler-Ids': '["b1df1310-4d5b-4000-8f43-e5b6f4958a48","b9dcdaa4-64ef-4be1-bbbe-90fe664a81bd","db1868eb-0a12-4217-a89a-5cbb3946b0e7","b8166dda-a74f-4e64-8bd4-fe529e95bf04","205f9250-b5eb-4cfd-b94c-976778be8f17","229360b9-37d4-456f-b030-9e56879d7c84"]', 'X-Frame-Options': 'SAMEORIGIN', 'Referrer-Policy': 'strict-origin-when-cross-origin', 'Set-Cookie': 'ARRAffinity=87506ffb959c51b2ba135ec75a7dffc3bc28e2948e5cb4ee012d8d916b147438;Path=/;HttpOnly;Domain=www.kaggle.com', 'Date': 'Sat, 06 Oct 2018 16:23:01 GMT'})
HTTP response body: {"code":401,"message":"Unauthorized"}

5 个答案:

答案 0 :(得分:2)

我认为比赛的名称是错误的。试试:

from kaggle.api.kaggle_api_extended import KaggleApi

api = KaggleApi('copy and paste kaggle.json content here')
api.authenticate()
files = api.competition_download_files("two-sigma-financial-news")

答案 1 :(得分:1)

在查看源代码时,我发现了此类。我认为笔记本在调用KaggleApi()时不会自动进行身份验证,因此您需要在API上调用身份验证功能以连接到Kaggle API。

尝试:

api = KaggleApi()
api.authenticate()

通话后,我能够连接并下载示例。

答案 2 :(得分:1)

您尚未为您的代码提供任何授权,例如您的用户ID,密码和最重要的身份验证密钥。身份验证密钥在用户ID和其Kaggle密码之后给出。

将Kaggle API分配给名为“ api”的变量后,可以从api.authenticate()函数获得Kaggle身份验证。

答案 3 :(得分:0)

您的usernamekey未提供或无效。

转到https://www.kaggle.com/username/account并创建新的API令牌。 kaggle.json文件将被下载。将其放在~/.kaggle/kaggle.jsonC:\Users\User\.kaggle\kggle.json中。

此外,您还必须在“规则接受”部分中单击“我理解并接受”以获取要下载的数据。

答案 4 :(得分:0)

这是从kaggle API导入数据的另一种Python方法。我假设您正在使用Linux OS进行云实例。

这是我的操作方式:

  1. 从kaggle帐户页面中获取kaggle.json文件:https://www.kaggle.com/<username>/account
  2. 运行此代码,并确保您在正确的目录中具有kaggle.json。

    import json 
    import os 
    os.chdir("~/.kaggle")
    data = {"username":"username","key":"tockenvalue"} # get this data from kaggle.json file 
    with open('kaggle.json', 'w') as outfile:
        json.dump(data, outfile)
    
  3. 在终端中的
  4. 上,cd将目录更改为要放置数据的位置,然后在终端中键入: kaggle competitions download -c two-sigma-financial-news

每次您想从Kaggle API导入数据时,此功能就可用。