未定义变量';YouTube';python
我正试图用我在网上找到的代码下载youtube播放列表,但我遇到了一个困扰我的错误。(我对Python一无所知。) 代码如下:未定义变量';YouTube';python,python,beautifulsoup,python-requests,re,Python,Beautifulsoup,Python Requests,Re,我正试图用我在网上找到的代码下载youtube播放列表,但我遇到了一个困扰我的错误。(我对Python一无所知。) 代码如下: import requests import re from bs4 import BeautifulSoup website = 'https://www.youtube.com/playlist?list=PLCReyWKrw9wdoyb6Z9aVhPS5r3ZTA33Ky' r= requests.get(website) soup = BeautifulSo
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
类型(或者可能是其他包)。您需要:
- 确保此软件包已安装在您的虚拟环境中(如果您未使用虚拟环境,请确保全局
)站点软件包
- 导入它
from pytube import YouTube
from pytube import YouTube