0

我编写了这段代码来制作 Python 视频下载器: from pytube import *

# 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

我如何解决它 ???

4

1 回答 1

2

您可能会看到错误,因为可能有一些异常,可能是您可以初始化yt = None然后检查它是否不是 None 然后可以做不同的事情。

我无法重现您的错误,但出现了另一个错误:

Copy and paste your link here: https://www.youtube.com/watch?v=n4RjJKxsamQ
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-0d28f5939137> in <module>
     12
     13
---> 14 mp4files = yt.filter('mp4')
     15 yt.set_filename = input('What do u want to name it: ')
     16 d_video = yt.get(mp4files[-1].extension, mp4files[-1].resolution)

AttributeError: 'YouTube' object has no attribute 'filter'

您可以filter通过查看Pytube 'YouTube' 对象没有属性 'filter'来解决问题

于 2021-12-27T12:25:31.203 回答