【问题标题】:Is there an specific way to treat error in loop python是否有一种特定的方法来处理循环python中的错误
【发布时间】:2022-01-21 07:09:42
【问题描述】:

我找到了一个笔记本,但我卡在了这一点上[10]:

import pandas as pd

使用 API 循环遍历轨道 URI 和拉取艺术家 URI, 然后使用艺术家 URI 提取与该艺术家关联的流派 将所有这些存储在字典中

     for t_uri in track_uris:
    
        dict_genre[t_uri] = {'artist_uri': "", "genres":[]}
    
        r = requests.get(BASE_URL + 'tracks/' + t_uri, headers=headers)
        r = r.json()
        a_uri = r['artists'][0]['uri'].split(':')[2]
        dict_genre[t_uri]['artist_uri'] = a_uri
    
        s = requests.get(BASE_URL + 'artists/' + a_uri, headers=headers)
        s = s.json()
        dict_genre[t_uri]['genres'] = s['genres']
KeyError                                  
Traceback (most recent call last) <ipython-input-43-3c5b75bf03e3> in <module>()
    14     r = requests.get(BASE_URL + 'tracks/' + t_uri, headers=headers)
    15     r = r.json()    
---> 16     a_uri = r['artists'][0]['uri'].split(':')[2]*    
KeyError: 'artists'

来源:

【问题讨论】:

  • 试试print(r)看看数据包含什么。
  • print(r) inside for output: {'error': {'status': 429, 'message': 'API rate limit exceeded'}} print(r) outside for output: {'error': {'status': 429, 'message': 'API rate limit exceeded'}} {'album': {'album_type': 'single', 'artists': [{'external_urls': {'spotify': *URL Links after this*
  • “超出速率限制”似乎是显而易见的问题。
  • @Barmar 谢谢,非常感谢您的帮助
  • 请澄清您的具体问题或提供更多详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python pandas dataframe spotify


【解决方案1】:

根据Spotify API,如果您使用artists 键没有得到响应,则有以下选项:

  • 401 错误。错误或过期的令牌。如果用户撤销了令牌或访问令牌已过期,则可能会发生这种情况。您应该重新对用户进行身份验证。
  • 403 错误。错误的 OAuth 请求(错误的使用者密钥、错误的 nonce、过期的时间戳......)。不幸的是,在这里重新对用户进行身份验证无济于事。
  • [对您来说不太可能] 429 错误。该应用已超出其速率限制。

否则,您将不会出现此错误。所以检查你的header,它包含一个不正确的令牌。

【讨论】:

    猜你喜欢
    • 2014-01-27
    • 1970-01-01
    • 2013-07-09
    • 2011-09-29
    • 2013-03-21
    • 1970-01-01
    • 2014-07-26
    • 2018-05-27
    • 2022-07-27
    相关资源
    最近更新 更多