首页 > 解决方案 > 为什么我在使用 pytube 时会收到这些错误?

问题描述

我的问题:在官方 Python IDLE 中使用 pytube 包时,我收到一长串错误。我只是在玩一个 python YouTube 视频下载器,我不太确定发生了什么。我有一个当前的 python 脚本,当它运行时,给了我这个:

Traceback (most recent call last):
  File "C:\Users\owenr\Desktop\Python Video Downloader.py", line 6, in <module>
    yt = YouTube(link)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 104, in __init__
    self.prefetch()
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 221, in prefetch
    self.vid_info_raw = request.get(self.vid_info_url)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\request.py", line 42, in get
    return _execute_request(url, headers=extra_headers).read().decode("utf-8")
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\request.py", line 26, in _execute_request
    return urlopen(request)  # nosec
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 555, in error
    result = self._call_chain(*args)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\owenr\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\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 561, in error
    return self._call_chain(*args)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\owenr\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 404: Not Found 

原始代码是,

from pytube import YouTube
import time
import sys
while True:
    link = input("Enter the link:")
    yt = YouTube(link)

    print("Information:")
    time.sleep(1.25)

    print("    Title: ", yt.title)
    print("    Number of views: ", yt.views)
    print("    Length of video: ", yt.views)
    print("    Ratings: ", yt.rating)
    time.sleep(1)
    
    yn = input("Is this correct? [Y / n]")
    if yn == "Y":
        print("Downloading...")
        stream = yt.streams.first()
        stream.download('C:\\Users\\owenr\\Desktop\\Python Videos')
        sys.exit("Status: Completed")
    if yn == "n":
        time.sleep(1)
        continue 

当我在 IDLE 中运行此程序时,我收到非常相似的错误,而不是来自“.py”文件(如上所示)。

>>> from pytube import YouTube
>>> yt = YouTube('https://www.youtube.com/watch?v=6TWJaFD6R2s')
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    yt = YouTube('https://www.youtube.com/watch?v=6TWJaFD6R2s')
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 104, in __init__
    self.prefetch()
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 221, in prefetch
    self.vid_info_raw = request.get(self.vid_info_url)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\request.py", line 42, in get
    return _execute_request(url, headers=extra_headers).read().decode("utf-8")
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\request.py", line 26, in _execute_request
    return urlopen(request)  # nosec
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 555, in error
    result = self._call_chain(*args)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\owenr\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\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 561, in error
    return self._call_chain(*args)
  File "C:\Users\owenr\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\owenr\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 404: Not Found

此示例中使用的链接完全是任意的。如您所见,错误仅在之后显示

yt = YouTube('https://www.youtube.com/watch?v=6TWJaFD6R2s')

如果有人可以帮助我弄清楚这里发生了什么以及如何解决它,那将不胜感激。

标签: pythonpytube

解决方案


这似乎是 PyTube 库的问题。这里有一个问题:https ://github.com/pytube/pytube/issues/495

看起来它最近已修复(截至 9 小时前),并在 27 分钟前发布了新版本。

我建议通过调用pip install pytube --upgrade并再次尝试您的代码来升级。


推荐阅读