使用pytube时,我一直收到错误:yt未定义

2024-05-02 13:41:28 发布

您现在位置:Python中文网/ 问答频道 /正文

在长时间休息之后,我正试图重新学习python,但无论我做什么,我都无法让这段代码正常工作。 它一直在告诉我

  File "/Users/fletch/PycharmProjects/untitled1/YT Download.py", line 18, in <module>
    mp4files = yt.filter('mp4')
NameError: name 'yt' is not defined

整个程序看起来像这样

from pip._vendor.distlib.compat import raw_input
import pytube

# save path
from pytube import YouTube

SAVE_PATH = "/Users/fletch/Movies"

# Get Video URL from User
url = raw_input("Please input the link of the Video you want to download: \n".rstrip())

try:
    yt = YouTube(url)
except:
    print("Connection Error")

# Filters Out MP4 Files
mp4files = yt.filter('mp4')

# Set File Name
filename = raw_input("Please name your file: \n".rstrip())
yt.set_filename(filename)

# Get the Video
d_video = yt.get(mp4files[-1].extension, mp4files[-1].resolution)
try:
    d_video.download(SAVE_PATH)
except:
    print("Unknown Error, Try Again Later")
print("Task Complete")

任何帮助都将不胜感激


Tags: thefromimportinputrawvideofilterfilename