【问题标题】:How to take user Input of file downloading path using pytube如何使用 pytube 获取用户输入的文件下载路径
【发布时间】:2021-08-04 22:48:26
【问题描述】:

我做了一个 Youtube 下载器,但无法根据用户输入的路径保存视频;下面是我的代码。请参考这个,我已经编辑了代码。我正在尝试制作一个 GUI YT 视频下载器,并且我已经导入了每个必备库,当我在 video.download(path) 中进行操作时,我得到一个错误类未定义,当我向其中添加引号时它会自动创建一个名为 PATH

的新文件夹
from tkinter import *
from pytube import YouTube
from tkinter import filedialog

root = Tk()
root.geometry('800x600')
root.resizable(0,0)
root.title("YouTube Video Downloader")
def browseFiles():
    path = filedialog.askdirectory()
    location = '"' + path + '"'
    print(location)


Label(root,text = 'Youtube Video Downloader', font ='arial 20 bold').pack()
Label(root , text = 'Choose directory' , font = 'arial 20 bold').place(x =120, y=190)

button_explore = Button(text = "Choose directory" , command = browseFiles)

link = StringVar()

Label(root, text = 'Paste your Link Here:', font = 'calibri 25 bold').place(x= 250 , y = 50)
link_enter = Entry(root, width = 90,textvariable = link).place(x = 120, y = 110)



#syntax to download video from youtube


def Downloader():
     
    link1 =YouTube(str(link.get()))
    video = link1.streams.filter(res = "1080p").first()
    video.download(r'path')
    Label(root, text = 'DOWNLOADED', font = 'calibri 20').place(x= 180 , y = 210)  


Button(root,text = 'DOWNLOAD', font = 'calibri 15 bold' ,bg = 'firebrick1', padx = 2, command = Downloader).place(x=300 ,y =350)
button_exit = Button(text = "Exit" , command = exit)
button_exit.place(x= 250,y=500)
button_explore.place(x = 120, y= 240)

root.mainloop()

【问题讨论】:

    标签: python python-3.x pytube


    【解决方案1】:

    如果没有更多信息,很难确切地知道原因,但我曾经遇到过类似的问题,我通过编写类似save(r'path') 的路径来解决它

    【讨论】:

    • 你能简单解释一下吗
    • “r”是字符串前缀,表示后面的字符串是原始字符串。您需要这个,以便 Windows 读取路径中的反斜杠。反斜杠否则是一个“转义字符”所以我会尝试写作 video.download(r'path')
    • Python Documentation 很好地解释了这些字符串前缀的作用。
    • 你好朱利安,我已经添加了我的完整代码,你可以参考它谢谢你的帮助,
    • 使用 'r' 前缀不起作用,它只是创建了一个名为 path 的新文件夹
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    相关资源
    最近更新 更多