PyTube, URLlib, Http packages giving error while trying to download youtube video

dknight Source

I am trying to download youtube video using PyTube package but I am unable to do so. I am using Python 3.5 and pytube 9.2.2. Urllib doesn't give me an error in other programs but it is giving me errors here.

The code I am using is:

#importing the module
from pytube import YouTube

#where to save
SAVE_PATH = "E:/" #to_do

#link of the video to be downloaded
link="https://www.youtube.com/watch?v=xWOoBJUqlbI"


yt = YouTube(link)


#filters out all the files with "mp4" extension
mp4files = yt.filter('mp4')

yt.set_filename('GeeksforGeeks Video') #to set the name of the file

#get the video with the extension and resolution passed in the get() function
d_video = yt.get(mp4files[-1].extension,mp4files[-1].resolution)
try:
    #downloading the video
    d_video.download(SAVE_PATH)
except:
    print("Some Error!")
print('Task Completed!')

The errors that I am getting are:

Traceback (most recent call last):
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 1240, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1083, in request
    self._send_request(method, url, body, headers)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1128, in _send_request
    self.endheaders(body)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1079, in endheaders
    self._send_output(message_body)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 911, in _send_output
    self.send(msg)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 854, in send
    self.connect()
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1237, in connect
    server_hostname=server_hostname)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\ssl.py", line 376, in wrap_socket
    _context=self)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\ssl.py", line 747, in __init__
    self.do_handshake()
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\ssl.py", line 983, in do_handshake
    self._sslobj.do_handshake()
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\ssl.py", line 628, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:646)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "2TryingPyTube.py", line 10, in <module>
    yt = YouTube(link)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\site-packages\pytube\__main__.py", line 87, in __init__
    self.prefetch_init()
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\site-packages\pytube\__main__.py", line 95, in prefetch_init
    self.prefetch()
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\site-packages\pytube\__main__.py", line 158, in prefetch
    self.watch_html = request.get(url=self.watch_url)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\site-packages\pytube\request.py", line 21, in get
    response = urlopen(url)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 465, in open
    response = self._open(req, data)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 483, in _open
    '_open', req)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 443, in _call_chain
    result = func(*args)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 1283, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 1242, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:646)>

I wish someone could help me. PS - I am only a beginner in using urllib, http packages. Please don't write harsh comments, thank you :0

pythonpython-3.xerror-handlingurllibpytube

Answers

comments powered by Disqus