鱼C论坛

 找回密码
 立即注册
查看: 128|回复: 1

“如果不存在”, 只能用try来避开?

[复制链接]
发表于 2024-3-25 14:40:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
if transcript_list.find_manually_created_transcript(['en']) is not None:
        print('man have-----------------')
elif transcript_list.find_generated_transcript(['en']):
        print('gen have -----------------')
else:
        continue


下面是告警
ytube_transcript_api._errors.NoTranscriptFound:
Could not retrieve a transcript for the video https://www.ytube.com/watch?v=DbkEDAqCxeo! This is most likely caused by:

No transcripts were found for any of the requested language codes: ['en']

For this video (DbkEDAqCxeo) transcripts are available in the following languages:

(MANUALLY CREATED)
None

(GENERATED)
- en ("English (auto-generated)")[TRANSLATABLE]

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-25 14:40:48 | 显示全部楼层
在处理这种不存在的情况时,使用`try-except`块是一种常见的方法。通过将可能引发异常的代码放在`try`块中,然后使用`except`块来处理异常。

对于你的代码,可以使用`try-except`块来处理找不到转录文本的异常。例如:

  1. try:
  2.     if transcript_list.find_manually_created_transcript() is not None:
  3.         print('man have-----------------')
  4.     elif transcript_list.find_generated_transcript():
  5.         print('gen have -----------------')
  6.     else:
  7.         continue
  8. except ytube_transcript_api._errors.NoTranscriptFound as e:
  9.     print(e)
复制代码


在上面的代码中,如果`find_manually_created_transcript()`或`find_generated_transcript()`抛出`ytube_transcript_api._errors.NoTranscriptFound`异常,程序将捕获该异常并打印出错误消息。

请注意,在你的代码中我用`e`代表异常对象,你可以根据需要对其进行处理,比如输出更详细的错误信息或采取其他适当的行动。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 18:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表