【发布时间】:2021-12-27 12:14:43
【问题描述】:
我编写了这段代码来制作 Python 视频下载器: 从 pytube 导入 *
# where to save
from pytube import YouTube
SAVE_PATH = "C:/Downloads"
# link of the video to be downloaded
link = input('Copy and paste your link here: ')
try:
yt: YouTube = YouTube(link)
except:
print("Connection Error")
mp4files = yt.filter('mp4')
yt.set_filename = input('What do u want to name it: ')
d_video = yt.get(mp4files[-1].extension, mp4files[-1].resolution)
try:
d_video.download(SAVE_PATH)
except:
print("Some Error!")
print('Task Completed!')
在我运行它之后,它要求我输入我希望它下载的链接,就像我希望它做的那样 在我输入链接后,它显示了这个错误:
mp4files = yt.filter('mp4')
NameError: name 'yt' is not defined
我该如何解决???
【问题讨论】:
-
您正在点击
except语句,因此未创建yt变量。当这种情况发生时,您需要有关您希望整个程序做什么的逻辑,例如退出? -
您在
try块中定义了yt,而不是作为全局变量。也许这就是你有这个问题的原因 -
可能在您的错误消息中更明确。看起来它抓住了你不想要的东西。认为连接错误引发
URLError
标签: python youtube undefined pytube yt