即使我抓住了它,但抛出了例外

发布于 2025-02-07 13:10:37 字数 1209 浏览 1 评论 0原文

我正在尝试列举收集URL,以使用。 当轨道ID不存在时,即使我正在捕获所述的确切错误,该程序也会引发错误。该错误说在处理异常时,发生了另一个例外,这是否意味着我设置了我的尝试不正确?

import spotipy
from spotipy.oauth2 import SpotifyOAuth

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="...",
                                               client_secret="...",
                                               redirect_uri="http://localhost/",
                                               scope="playlist-modify-public",
                                               requests_timeout=5))
try:
    track = sp.track('7IaxPh0ttHTsJ4rVPPiQ9V', 'NL')
except sp.SpotifyException:
    x = 0

这是我收到的错误消息:

Exception has occurred: SpotifyException
http status: 404, code:-1 - https://api.spotify.com/v1/tracks/7IaxPh0ttHTsJ4rVPPiQ9V?market=NL:
 Non existing id: 'spotify:track:7IaxPh0ttHTsJ4rVPPiQ9V', reason: None

During handling of the above exception, another exception occurred:

  File "C:\Users\Dirkv\OneDrive\Documents\Projects\SelfStudy\Python\Music Bot\music_bot.py", line 38, in <module>
    track = sp.track(url, 'NL')

I'm trying to enumerate a collection URLs to get corresponding spotify tracks using Spotipy.
When a track ID does not exist, the program throws an error, even though I'm catching the exact error mentioned. The error says that during handling of the exception another exception occured, does this mean I've setup my try catch incorrectly?

import spotipy
from spotipy.oauth2 import SpotifyOAuth

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="...",
                                               client_secret="...",
                                               redirect_uri="http://localhost/",
                                               scope="playlist-modify-public",
                                               requests_timeout=5))
try:
    track = sp.track('7IaxPh0ttHTsJ4rVPPiQ9V', 'NL')
except sp.SpotifyException:
    x = 0

This is the error message I get:

Exception has occurred: SpotifyException
http status: 404, code:-1 - https://api.spotify.com/v1/tracks/7IaxPh0ttHTsJ4rVPPiQ9V?market=NL:
 Non existing id: 'spotify:track:7IaxPh0ttHTsJ4rVPPiQ9V', reason: None

During handling of the above exception, another exception occurred:

  File "C:\Users\Dirkv\OneDrive\Documents\Projects\SelfStudy\Python\Music Bot\music_bot.py", line 38, in <module>
    track = sp.track(url, 'NL')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚你心 2025-02-14 13:10:37

您需要从 spotipy 导入错误,而不是 sp


import spotipy
from spotipy.oauth2 import SpotifyOAuth

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="...",
                                               client_secret="...",
                                               redirect_uri="http://localhost/",
                                               scope="playlist-modify-public",
                                               requests_timeout=5))

try:
    track = sp.track('7IaxPh0ttHTsJ4rVPPiQ9V', 'NL')
except spotipy.SpotifyException :
    x = 0
except spotipy.SpotifyOauthError:
    print("SpotifyOauthError")

注意:

请勿发布您的 client_id client_secret 为了您自己的安全

You need to import errors from spotipy not from sp


import spotipy
from spotipy.oauth2 import SpotifyOAuth

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="...",
                                               client_secret="...",
                                               redirect_uri="http://localhost/",
                                               scope="playlist-modify-public",
                                               requests_timeout=5))

try:
    track = sp.track('7IaxPh0ttHTsJ4rVPPiQ9V', 'NL')
except spotipy.SpotifyException :
    x = 0
except spotipy.SpotifyOauthError:
    print("SpotifyOauthError")

NOTE :

DO NOT post your client_id or client_secret for your own safety

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文