Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I download YouTube videos with Python? [closed]

I want to download YouTube videos with Python. I tried opencv with Python, but there was always a problem. I am trying to use pytube3 and Python 3. I tried this program (that I found on StackOverflow):

from pytube import YouTube
YouTube('http://youtube.com/watch?v=9bZkp7q19f0').streams.first().download()

But it failed with the "HTTP Error 410" error. I keep getting "HTTP Error 410" when I run various pytube programs. I changed the URL and tested the URLs in a browser. Nothing works when I run a Python program to download the video.

I am trying to run this program (as I found it here):

from pytube import YouTube
 
try:
    yt_obj = YouTube('https://www.youtube.com/watch?v=DkU9WFj8sYo')
 
    print(f'Video Title is {yt_obj.title}')
    print(f'Video Length is {yt_obj.length} seconds')
    print(f'Video Description is {yt_obj.description}')
    print(f'Video Rating is {yt_obj.rating}')
    print(f'Video Views Count is {yt_obj.views}')
    print(f'Video Author is {yt_obj.author}')
 
except Exception as e:
    print(e)

I got this:

HTTP Error 410: Gone Traceback (most recent call last): File "e.py", line 9, in yt_obj = YouTube(url) File "/home/jdoe/path_dir/lib/python3.8/site-packages/pytube/main.py", line 91, in init self.prefetch() File "/home/jdoe/path_dir/lib/python3.8/site-packages/pytube/main.py", line 181, in prefetch self.vid_info_raw = request.get(self.vid_info_url) File "/home/jdoe/path_dir/lib/python3.8/site-packages/pytube/request.py", line 36, in get return _execute_request(url).read().decode("utf-8") File "/home/jdoe/path_dir/lib/python3.8/site-packages/pytube/request.py", line 24, in _execute_request return urlopen(request) # nosec File "/usr/lib/python3.8/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python3.8/urllib/request.py", line 531, in open response = meth(req, response) File "/usr/lib/python3.8/urllib/request.py", line 640, in http_response response = self.parent.error( File "/usr/lib/python3.8/urllib/request.py", line 563, in error result = self._call_chain(*args) File "/usr/lib/python3.8/urllib/request.py", line 502, in _call_chain result = func(*args) File "/usr/lib/python3.8/urllib/request.py", line 755, in http_error_302 return self.parent.open(new, timeout=req.timeout) File "/usr/lib/python3.8/urllib/request.py", line 531, in open response = meth(req, response) File "/usr/lib/python3.8/urllib/request.py", line 640, in http_response response = self.parent.error( File "/usr/lib/python3.8/urllib/request.py", line 569, in error return self._call_chain(*args) File "/usr/lib/python3.8/urllib/request.py", line 502, in _call_chain result = func(*args) File "/usr/lib/python3.8/urllib/request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 410: Gone

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "e.py", line 14, in raise Exception('Some exception occurred.') Exception: Some exception occurred.

My research says that the 410 error involves a URL being found but then moved. As the problem happens with different youtube links and different programs, I am at a loss. How can I avoid getting Error 410 messages when using pytube?

like image 204
Ryan358 Avatar asked Dec 30 '22 13:12

Ryan358


1 Answers

There is a bug in pytube3 that results in the 410 error. There is a fix linked from that issue that appears to resolve the problem. If I install the patched version:

pip install git+https://github.com/Zeecka/pytube@fix_1060#egg=pytube

Then I can fetch information about youtube videos without that error:

>>> import pytube
>>> yt = pytube.YouTube('https://www.youtube.com/watch?v=DkU9WFj8sYo')
>>> yt.title
'Space Ranger Video Game using FXGL and JavaFX'

The fix has been submitted as a pull request. Reading through the discussion it looks like there may still be some unresolved issues.

like image 61
larsks Avatar answered Jan 02 '23 04:01

larsks