Reputation: 901
I use youtube-dl in python, sometime I got ContentTooShortError, how can I use "try...catch..." in python to deal with these exceptions?
I use this code but it doesn't work
with youtube_dl.YoutubeDL(options) as ydl:
# youtube_url = video.youtube_url
n = 0
try:
# 用设置成list的形式
ydl.download([video.youtube_url])
except 'ContentTooShortError':
if n + 1 < max_retey:
ydl.download([video.youtube_url])
else:
return False
Upvotes: 3
Views: 2617
Reputation: 12613
Remove the quotes. The exception class need not be added as a string.
except ContentTooShortError:
And as mentioned by @MichalPawlowski in the comments, make sure you import it.
# For Python 3
from urllib.error import ContentTooShortError
# For Python 2
from urllib import ContentTooShortError
Upvotes: 3