【问题标题】:Pytube Error in Python for downloading Youtube Video用于下载 Youtube 视频的 Python 中的 Pytube 错误
【发布时间】:2021-08-08 15:39:54
【问题描述】:

我在制作使用 GUI (Tkinter ttk) 下载 YouTube 视频的程序时遇到此错误 请有人帮助我:( 我尝试了很多 stackoverflow 查询,但没有任何帮助,甚至我从 Github 复制了初始代码,但错误仍然存​​在于我的系统中,但是代码在参考人处正常运行 也许是它的连接错误或者我有 2 个 chrome 扩展的东西: 1.触摸VPN 2.元掩码

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "f:\Sumant\Python\YouTube Downloader\yt.py", line 25, in download
    yt = YouTube(url)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 91, in __init__
    self.prefetch()
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 181, in prefetch
    self.vid_info_raw = request.get(self.vid_info_url)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\request.py", line 36, in get
    return _execute_request(url).read().decode("utf-8")
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\request.py", line 24, in _execute_request
    return urlopen(request)  # nosec
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 555, in error
    result = self._call_chain(*args)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 747, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 561, in error
    return self._call_chain(*args)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 641, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 410: Gone

代码

def download():
    url = ent_link.get()
    res = var.get()

    if len(url) < 1:
        messagebox.showerror("Error", "URL cannot be Empty")
 
    yt = YouTube(url)
    try:
        if var.get() == 0:
             reso_select = yt.streams.get_highest_resolution()
        elif var.get() == 1:
            reso_select = yt.streams.get_lowest_resolution()
        elif var.get() == 2:
            reso_select = yt.streams.filter(only_audio=True).first()
        else:
            reso_select = yt.streams.get_highest_resolution()
        try:
            reso_select.download(path1)
            messagebox.showinfo("Sucess", "Video Downloaded!")
        except:
            messagebox.showerror("Error", "Download Failed")
    except:
        messagebox.showerror("Error","Please try again")

可能yt = YouTube(url)行有错误

完整代码GitHub Repository

【问题讨论】:

  • “HTTP 错误 410:已消失”表示该资源已从其服务器中删除。除非他们发送这个来搞乱抓取工具,否则这意味着这不是您的代码的问题。您正在尝试获取一个不再存在的视频。
  • 这也不是RuntimeError。这是一个urllib.error.HTTPError。所以请删除问题上的runtime-error标签。
  • 另外,不要使用except:。这似乎不是你现在的问题,但是像这样的裸except 案例会在某个时候咬你。始终指定要捕获的异常。
  • @Carcigenicate youtube.com/watch?v=Lynx3vZz5Ao&t=1s 有结帐视频,您可以使用 GitHub 链接并使用代码从 YT 下载任何东西
  • 该代码在使用 pytube 11.0.0 和 Python 3.8.10 的 youtube 链接上运行良好。

标签: python tkinter urllib pytube


【解决方案1】:

这是 pytube 的一个错误,它以前发生过并已修复,但看起来它可能已被重新引入。您可以随时查看另一个库,例如youtube_dl 或 ytpy

【讨论】:

    【解决方案2】:

    我的错误已解决,是 pytube 模块出错了 如果有人看到任何代码的 HTTP 错误,请使用 2 个测试用例:

    1. 正确检查链接
    2. 重新安装 pytube(如果 pytube 不起作用安装 pytube3,但在卸载 pytube 之后)

    谢谢大家!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-12
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多