【问题标题】:Pytube connection error even when i'm connected即使我已连接,Pytube 连接错误
【发布时间】:2020-06-10 04:27:37
【问题描述】:

我编写了以下脚本来使用 pytube API 从 youtube 下载 mp4 视频:

from pytube import YouTube
import sys 


SAVE_PATH = input('Enter a saving path: ') 
LINK = input('Enter the link of the video: ')

try:
    yt = YouTube(LINK)
except:
    print('Connection error') 

print(yt.title)
items = yt.streams.filter(only_audio=True).all()
stream = items[0]

stream.download()

当我运行代码时,我得到以下错误:

Connection error                                                                          
Traceback (most recent call last):                                                          
File "C:\Users\Dell\Desktop\Python\youtube.py", line 24, in <module>                        
  print(yt.title)                                                                       
NameError: name 'yt' is not defined 

有什么问题,我该如何解决?

【问题讨论】:

    标签: python-3.x pytube


    【解决方案1】:

    问题在于您提供给程序的video-link,如果视频链接不正确或互联网连接不正确,yt 将不会被初始化,并且当它未初始化时您无法使用它。

    1. 尝试使用预定义的链接而不接受用户的输入,例如

      LINK= #any youtube-video link

    2. 如果您的程序在提供预定义链接后失败,则您的 Internet 连接不正确。

    【讨论】:

      【解决方案2】:

      应该是yt = pytube.YouTube(LINK)

      【讨论】:

        【解决方案3】:

        我认为这个问题的答案是每次提到“yt”变量都应该放在“try”块中

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-29
          • 1970-01-01
          • 2018-07-11
          • 1970-01-01
          • 2022-11-21
          • 1970-01-01
          • 1970-01-01
          • 2011-11-14
          相关资源
          最近更新 更多