#๐ error handling an except
73 messages ยท Page 1 of 1 (latest)
@wide tangle
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
def fetch_video_transcript(video_id):
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
youtube_transcript = " ".join([item['text'] for item in transcript]).replace('\n', ' ')
return youtube_transcript
except NoTranscriptFound:
print(f"Geen transcript gevonden voor video {video_id}.")
return "Transcript niet beschikbaar"
except TranscriptsDisabled:
print(f"Transcript is uitgeschakeld voor video {video_id}.")
return "Transcript uitgeschakeld"
except VideoUnavailable:
print(f"Video {video_id} is niet beschikbaar.")
return "Video niet beschikbaar"
except Exception as e:
print(f"Fout bij het ophalen van transcript voor video {video_id}: {e}")
return None
for video_file in video_files:
video_id = video_file.split('.')[0]
video_url = youtube_base_url + video_id
youtube_transcript = fetch_video_transcript(video_id)
sentimentwaarde = analyze_sentiment(youtube_transcript)
print(youtube_transcript[:50])
print(sentimentwaarde)
if not youtube_transcript:
print(f"Leeg of niet-beschikbaar transcript voor video {video_id}")
continue
if sentimentwaarde is None:
print(f"Geen sentimentwaarde beschikbaar voor video {video_id}")
continue
Fout bij het ophalen van transcript voor video HsmOJ4Xbfp4: no element found: line 1, column 0
Error analyzing sentiment: 'NoneType' object has no attribute 'lower'
Er is een fout opgetreden: 'NoneType' object is not subscriptable
just wondering how i can manage this better have a lot of videos so doesn t matter if one fails ๐
It's generally not a good idea to return multiple types from a function; especially when you're using the same types for success and failure cases. Here, you're returning strings for some error cases, None for the catch-all case, and a string in the success case. Does it even make sense to pass the error strings to analyze_sentiment? Why would you analyze the sentiment of an error message?
I'd probably move the try out of fetch_video_transcript and into the second block of code, and only run analyze_sentiment and such in the success case.
don t understand what you are trying to say the biggest problem seems to be that is doesn t continue after the func get d an error in the loop
Ya, because you're passing a None to analyze_sentiment, which it apparently can't handle. You need to check what the return of fetch_video_transcript is before passing it to analyze_sentiment.
And because you're using the same type (strings) for both error and success cases, it will likely be impossible to differentiate between success and failure accurately by just looking at the strings.
I don't think it makes sense for fetch_video_transcript to catch the errors since it is unable to properly handle the error cases anyway. You should only catch errors when/where they can be handled.
No.
I meant:
def fetch_video_transcript(video_id):
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
youtube_transcript = " ".join([item['text'] for item in transcript]).replace('\n', ' ')
return youtube_transcript
for video_file in video_files:
video_id = video_file.split('.')[0]
video_url = youtube_base_url + video_id
try:
youtube_transcript = fetch_video_transcript(video_id)
sentimentwaarde = analyze_sentiment(youtube_transcript)
print(youtube_transcript[:50])
print(sentimentwaarde)
except NoTranscriptFound:
print(f"Geen transcript gevonden voor video {video_id}.")
. . . # All the other 'except's if you want special error messages.
There, now fixed
why is sentiment in the yt transcript 
Which part? I'm not sure what you mean.
print(sentimentwaarde)
I just copied that part from your code: #1299491495807684638 message
and in what way will this prevent
Fout bij het ophalen van transcript voor video jx4q82a6jHc: no element found: line 1, column 0
Error analyzing sentiment: 'NoneType' object has no attribute 'lower'
Er is een fout opgetreden: 'NoneType' object is not subscriptable
because it still gets stuck on the error?
they're just telling you to wrap the call to analyze_sentiment in a try-except
Because now it's impossible for fetch_video_transcript to return None, and that error is caused by fetch_video_transcript returning None.
but what if i just remove None then from the func i have
Well, even if you replace it with a string, then you'd be passing an error string to analyze_sentiment, and why would you analyze the sentiment of an error message?
why not just move this code above the call to analyze_sentiment?
if not youtube_transcript:
print(f"Leeg of niet-beschikbaar transcript voor video {video_id}")
continue

so you want it too stop when it can t fetch because sentiment then doesn t even matter?
Why do you think it would stop? Note how I put a try in the for video_file in video_files: loop.
i mean i tw wouldn t need to check the ssentiment if the transcript doesn t exist
c/p this and let us know
for video_file in video_files:
video_id = video_file.split('.')[0]
video_url = youtube_base_url + video_id
youtube_transcript = fetch_video_transcript(video_id)
if not youtube_transcript:
print(f"Leeg of niet-beschikbaar transcript voor video {video_id}")
continue
sentimentwaarde = analyze_sentiment(youtube_transcript)
print(youtube_transcript[:50])
print(sentimentwaarde)
if sentimentwaarde is None:
print(f"Geen sentimentwaarde beschikbaar voor video {video_id}")
continue
Although this will check the sentiment of error strings, which seems like an odd thing to do. I can't yet tell if they're intending that behavior.
i don't disagree ๐
I don't know what you mean by this.
but why will that avoid the errors and this won t because looks pretty similair
like if transcript can be loaded sentiment won t work cuz it is based of that
because fetch_video_transcript returns None on a general exception and you pass None to analyze_sentiment which is expecting a string
Do you want it to check sentiment if the transcript can't be found?
but what is different in your code don t understand
i moved the test for youtube_transcript being falsy to before the call to analyze_sentiment instead of after it
doesn t have to can still check ig but just wanna make sure the whole script runs first before randomly breaking
test explicitly for is None if it makes more sense to you
huh but i have it also before ?
where? your original code has if not youtube_transcript after
If you don't want to check sentiment when the transcript can't be found, look back on the first code I suggested: #1299491495807684638 message
With that code, if fetch_video_transcript throws an exception when a transcript can't be found, the try in the for in the loop will catch that error, and the call to analyze_sentiment will be skipped.
it is above the sentiment
this is the original code you pasted
for video_file in video_files:
video_id = video_file.split('.')[0]
video_url = youtube_base_url + video_id
youtube_transcript = fetch_video_transcript(video_id)
sentimentwaarde = analyze_sentiment(youtube_transcript)
print(youtube_transcript[:50])
print(sentimentwaarde)
if not youtube_transcript:
print(f"Leeg of niet-beschikbaar transcript voor video {video_id}")
continue
if sentimentwaarde is None:
print(f"Geen sentimentwaarde beschikbaar voor video {video_id}")
continue
it is certainly after and not before
? transcript is above dunno what you mean
Ha, thanks.
I'd need to see more context, but this still has the issue where error strings will be passed to analyze_sentiment, which is why I recommended moving error handling out of the fetch function.
The way the other guy suggested only works well if None is returned in all error cases (which I suspect they were leading you towards).
but still don t understand what different in his code and he didn t wanna say
๐
#1299491495807684638 message
Their code does not call analyze_sentiment if youtube_transcript is falsey/None.
The continue causes it to be skipped.
but should i try this what i have now? pr the other thingy?
all i did is move this code up
this assumes you don't care that you're passing those error strings to analyze_sentiment
if you do care about that, then do it as carc is suggesting
alr seems to work thank you just scared it breaks later cuz my code sometimes also ran fine ๐
is there any chance it will break alter with the same code?
alr will close it thank you ๐
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.