Arun Kumar
Arun Kumar

Reputation: 31

pytube not working even after upgrading pytube and pip

I was trying to download audio of youtube video using pytube library but it's not working anymore tried to update pytube still not working.

from pytube import YouTube
YouTube('https://www.youtube.com/watch?v=HL9j8xuQ6Wc').streams.first().download()

The error I am getting

Traceback (most recent call last):
  File "/home/ideapoke/Documents/financial_analysis/youtube_to_audio.py", line 31, in <module>
    YouTube('https://www.youtube.com/watch?v=HL9j8xuQ6Wc').streams.first().download()
  File "/home/ideapoke/.local/lib/python3.10/site-packages/pytube/__main__.py", line 91, in __init__
    self.prefetch()
  File "/home/ideapoke/.local/lib/python3.10/site-packages/pytube/__main__.py", line 181, in prefetch
    self.vid_info_raw = request.get(self.vid_info_url)
  File "/home/ideapoke/.local/lib/python3.10/site-packages/pytube/request.py", line 36, in get
    return _execute_request(url).read().decode("utf-8")
  File "/home/ideapoke/.local/lib/python3.10/site-packages/pytube/request.py", line 24, in _execute_request
    return urlopen(request)  # nosec
  File "/usr/lib/python3.10/urllib/request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.10/urllib/request.py", line 525, in open
    response = meth(req, response)
  File "/usr/lib/python3.10/urllib/request.py", line 634, in http_response
    response = self.parent.error(
  File "/usr/lib/python3.10/urllib/request.py", line 557, in error
    result = self._call_chain(*args)
  File "/usr/lib/python3.10/urllib/request.py", line 496, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.10/urllib/request.py", line 749, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/usr/lib/python3.10/urllib/request.py", line 525, in open
    response = meth(req, response)
  File "/usr/lib/python3.10/urllib/request.py", line 634, in http_response
    response = self.parent.error(
  File "/usr/lib/python3.10/urllib/request.py", line 563, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.10/urllib/request.py", line 496, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.10/urllib/request.py", line 643, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 410: Gone

Upvotes: 3

Views: 3080

Answers (3)

RYON
RYON

Reputation: 53

the lib ur using is blocked by youtube so here's a fork that works just fine <3 first uninstall the old lib by doing

pip uninstall pytube

then u install the new fork by doing

pip install git+https://github.com/kszczepanskidev/pytube

and have fun bro <3

Upvotes: 3

shinobi
shinobi

Reputation: 11

Pytube is broken now, try pytubefix instead.

For Windows: pip install pytubefix

For Linux/Mac: pip3 install pytubefix

Upvotes: 1

Damir Talipov
Damir Talipov

Reputation: 119

Instead you can do like this

from pytube import YouTube

yt = YouTube("<Your URL>")

audio = yt.streams.filter(only_audio=True).first()

audio.download()

And change the following file in virtual environment folder (venv)

venv/lib/python3.10/site-packages/pytube/cipher.py

locate the function "get_throttling_function_name" in cipher.py and change its function_patterns to this

def get_throttling_function_name(js: str) -> str:
    function_patterns = [
        r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&.*?\|\|\s*([a-z]+)',
        r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])\([a-z]\)',
    ]

How to download audio youtube through pytube source

solution to error in case if you want to use approach that described in first link

Upvotes: 5

Related Questions