首页 > 解决方案 > 我需要帮助我的 YouTube 视频下载器在 Python 中使用 tkinter 和 pytube

问题描述

这是我的代码,我一直收到错误,但我不知道错误是什么以及为什么会出现错误

from pytube import YouTube
from tkinter import *
from tkinter import Tk, font

root = Tk()
root.geometry('700x250')
root.title('YouTube Downloader')

link_here = Label(root, text="Paste Here", font=("courier",40,))
link_here.place(x=220,y=40)

linkbruh = StringVar()
pasted = Entry(root,width=60,textvariable=linkbruh)
pasted.place(x=80,y=120)

def download():
    a = (str(linkbruh.get()))
    vid = YouTube(a).streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
    vid.download('/downloadhere')

Button(root,text="Download Video", width=20, bg="black", fg="gray", command=download).place(x=250,y=170)

root.mainloop()

那是我的代码,当我尝试粘贴 YouTube 视频的链接时,它只会给出一条错误消息

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1350, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1240, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1286, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1235, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1006, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 946, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1409, in connect
    self.sock = self._context.wrap_socket(self.sock,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
    return self.func(*args)
  File "/Users/dylanbradley/PycharmProjects/ytconvert/main.py", line 18, in download
    vid = YouTube(a).streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
  File "/Users/dylanbradley/PycharmProjects/ytconvert/venv/lib/python3.8/site-packages/pytube/__main__.py", line 91, in __init__
    self.prefetch()
  File "/Users/dylanbradley/PycharmProjects/ytconvert/venv/lib/python3.8/site-packages/pytube/__main__.py", line 162, in prefetch
    self.watch_html = request.get(url=self.watch_url)
  File "/Users/dylanbradley/PycharmProjects/ytconvert/venv/lib/python3.8/site-packages/pytube/request.py", line 36, in get
    return _execute_request(url).read().decode("utf-8")
  File "/Users/dylanbradley/PycharmProjects/ytconvert/venv/lib/python3.8/site-packages/pytube/request.py", line 24, in _execute_request
    return urlopen(request)  # nosec
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1393, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1353, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)>

我很新,我很困惑,如果你能帮助我,那将非常感激,因为我不明白这意味着什么

标签: pythontkinterpytube

解决方案


在这里试试这个:

from pytube import YouTube
from tkinter import *
from tkinter import Tk, font



def download():
    hide()
    a = (str(linkbruh.get()))
    try:
        vid = YouTube(a)
        down = vid.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
              
        title.config(text=f'title: {vid.title}')
        title.place(x=220, y=230)   

        
        down.download(r'\downloads')   # give your path here              

        error.config(text='Successfully downloaded')
        error.place(x=220, y=210)

        
    except:
        error.config(text='error: Unable to download the video')
        error.place(x=220, y=210)
        return 

def hide():
    title.place_forget()
    error.place_forget()

root = Tk()
root.geometry('700x250')
root.title('YouTube Downloader')

link_here = Label(root, text="Paste Here", font=("courier",40,))
link_here.place(x=220,y=40)

title = Label(root, text='')
title.place(x=220, y=230)

error = Label(root, text='')
error.place(x=220, y=210)

linkbruh = StringVar()
pasted = Entry(root,width=60,textvariable=linkbruh)
pasted.place(x=80,y=120)

Button(root,text="Download Video", width=20, bg="black", fg="gray", command=download).place(x=250,y=170)

root.mainloop()

推荐阅读