【发布时间】:2022-11-16 23:49:24
【问题描述】:
我是初学者,我有一个脚本可以通过youtube-search-python包的搜索查询在 YouTube 上查找视频
我怎样才能排除这个错误Error: list index out of range?
代码:
from youtubesearchpython import VideosSearch
class YoutubeSample:
def get_link(self, *args):
try:
producer_name, search = args[0]
videos_search = VideosSearch(producer_name + search, limit=1).result()
print("Youtube link for: ", producer_name, search, "Name of video: ", videos_search['result'][0]['title'])
return videos_search
except Exception as e:
print("Error: ", e)
return None
输出:
Youtube link for: PewDiePie
Error: list index out of range
Youtube link for: Mr Beat
Error: list index out of range
【问题讨论】:
-
list index out of range当您调用list[0]时可能意味着该列表为空。