【问题标题】:Kaggle datasets into jupyter notebookKaggle 数据集到 jupyter notebook
【发布时间】:2018-12-01 23:58:02
【问题描述】:

我正在尝试将一些数据从 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"}

【问题讨论】:

  • 如果您不提供某种类型的授权信息,API 如何知道您已接受条件?
  • 老实说,我有点不确定自己在做什么,我只是想将 kaggle 数据导入 Jpyter 记事本。
  • 我明白了,那么您遵循了哪些文档来执行此操作?
  • 我一直在关注 kaggle 的一些内核

标签: python kaggle


【解决方案1】:

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

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")

【讨论】:

  • 这似乎没有抛出任何错误,但我找不到原始的 Kaggle 帖子来处理数据以确保它正确导入。
【解决方案2】:

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

试试:

api = KaggleApi()
api.authenticate()

在这次通话之后,我能够连接并下载示例。

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
【解决方案3】:

这是另一种从 kaggle API 导入数据的 Python 方式。我假设您正在使用 linux 操作系统处理云实例。

这是我的做法:

  1. 从您的 kaggle 帐户页面获取您的 kaggle.json 文件:https://www.kaggle.com/&lt;username&gt;/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. 在终端中,cd 将目录更改为要放置数据的位置,然后输入终端: kaggle competitions download -c two-sigma-financial-news

每次你想从 Kaggle API 导入数据时都可以使用。

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      您的usernamekey 未提供或无效。

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

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

      【讨论】:

      • 我为之前遇到的问题做了这个,它解决了那个问题,现在我遇到了一个未经授权的问题。
      • 您必须在“规则接受”部分中单击“我理解并接受”才能下载要下载的数据。你接受了吗?
      • 我已经接受了规则,我可以下载数据了。我只是不能直接从 kaggle 网站导入它。
      • @user113156 “我就是不能直接从 kaggle 网站导入”——这是什么意思?
      • 我的印象是from kaggle.competitions import twosigmanews允许我直接从kaggle下载数据?
      猜你喜欢
      • 2020-04-29
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多