【问题标题】:How can I deal with this "Error: list index out of range"?我该如何处理这个“错误:列表索引超出范围”?
【发布时间】: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] 时可能意味着该列表为空。

标签: python youtube


【解决方案1】:

您尝试从一个序列中解包太多值。

替换这一行

producer_name, search = args[0]

有了这个。

producer_name, search = args

像这样打电话:

YoutubeSample().get_link("producer", "PewDiePie")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    相关资源
    最近更新 更多