【问题标题】:Line 7, IndexError: list index out of range第 7 行,IndexError:列表索引超出范围
【发布时间】:2021-01-15 19:23:43
【问题描述】:

请帮我解决这个问题,这是我已经尝试过的代码。 非常感谢您的帮助。

import urllib.request
import re

search_keyword="ill%20wiat"
html = urllib.request.urlopen("https://www.youtube.com/results?search_query=" + search_keyword)
video_ids = re.findall(r"watch?v=(\S{11})", html.read().decode())
print("https://www.youtube.com/watch?v=" + video_ids[0])

【问题讨论】:

  • 检查video_ids的值以确保它不为空,或者None,您可以通过print(video_ids)进行检查
  • video_ids 列表的输出为空 (print (len(video_ids))。它没有任何要访问的元素。if video_ids: print("youtube.com/watch?v=" + video_ids[0]) else:打印(“空”)
  • 请提供完整的追溯。
  • 这能回答你的问题吗? IndexError: list index out of range and python
  • Tejesh:应更新此问题以包含所需行为、特定问题或错误以及重现问题所需的最短代码。

标签: python youtube


【解决方案1】:

首先检查您尝试解析的页面。你写道:

r"watch?v=(\S{11})"

记得吗?这里的 char 将被解析为 REGEX 运算符,而不是你想要的字符串, 所以首先你需要这样写:

/watch[?]v=(\S{11})

因此您的正则表达式将被正确解析
第二:打印列表以查看您获得的内容并使用 FOR 循环而不是直接访问索引 [0] 通过列表进行迭代的良好做法。
在您的情况下,您会因为您的 id 列表为空而收到此错误。

下一个代码对我有用

import urllib.request
import re

search_keyword="ill%20wiat"
url="https://www.youtube.com/results?search_query="+search_keyword
with urllib.request.urlopen(url) as response:
   video_ids = re.findall("/watch[?]v=(\S{11})", response.read().decode())
   for video in video_ids:
      print("https://www.youtube.com/watch?v=" + video)

P.S 不要用 try/except 包装你的代码来捕获此类抛出的错误

【讨论】:

    【解决方案2】:

    urllib 不会给你数据 使用

    import requests
    html=requests.get('https://www.youtube.com/results?search_query='+search_keyword)
    text=html.text
    

    文本包含所有 html 数据,因此从文本中搜索

    【讨论】:

    • 但它返回youtube的整个html代码,如何只获取youtube url
    • 使用beautifulsoup 进行html parcing
    猜你喜欢
    • 1970-01-01
    • 2011-10-31
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多