未定义变量 'YouTube' python

Undefined variable 'YouTube' python

我正在尝试使用我在网上找到的代码下载 youtube 播放列表,但出现了一个困扰我的错误。 (我对Python一无所知。)

代码如下:

import requests
import re
from bs4 import BeautifulSoup
 
website = 'https://www.youtube.com/playlist?list=PLCReyWKrw9wdoyb6Z9aVhPS5r3ZTA33Ky'
r= requests.get(website)
soup = BeautifulSoup(r.text, features="html.parser")
 
tgt_list = [a['href'] for a in soup.find_all('a', href=True)]
tgt_list = [n for n in tgt_list if re.search('watch',n)]
 
unique_list= []
for n in tgt_list:
    if n not in unique_list:
        unique_list.append(n)
 
# all the videos link in a playlist
unique_list = ['https://www.youtube.com' + n for n in unique_list]
 
for link in unique_list:
    print(link)
    y = YouTube(link)
    t = y.streams.all()
    t[0].download(output_path=r"C:\Users\Catalin\Desktop\New folder (2)")

错误:

 {
    "resource": "/c:/Users/Catalin/Desktop/Test/Test.py",
    "owner": "python",
    "code": "undefined-variable",
    "severity": 8,
    "message": "Undefined variable 'YouTube'",
    "source": "pylint",
    "startLineNumber": 22,
    "startColumn": 9,
    "endLineNumber": 22,
    "endColumn": 9
}

我应该如何声明该变量,或者我必须具体做什么?

您正在调用 YouTube(link),但此函数并未出现在您的代码中。

推测您正在尝试使用 pytube 中的 YouTube 类型(或可能是其他包)。您需要:

  • 确保此软件包安装在您的虚拟环境中(如果您不使用虚拟环境,则安装在全局 site-packages 中)

    pip install pytube
    
  • 导入...

    from pytube import YouTube