15

I just downloaded pytube (version 11.0.1) and started with this code snippet from here:

from pytube import YouTube
YouTube('https://youtu.be/9bZkp7q19f0').streams.first().download()

which gives this error:

AttributeError                            Traceback (most recent call last)
<ipython-input-29-0bfa08b87614> in <module>
----> 1 YouTube('https://youtu.be/9bZkp7q19f0').streams.first().download()

~/anaconda3/lib/python3.8/site-packages/pytube/__main__.py in streams(self)
    290         """
    291         self.check_availability()
--> 292         return StreamQuery(self.fmt_streams)
    293 
    294     @property

~/anaconda3/lib/python3.8/site-packages/pytube/__main__.py in fmt_streams(self)
    175         # https://github.com/pytube/pytube/issues/1054
    176         try:
--> 177             extract.apply_signature(stream_manifest, self.vid_info, self.js)                                                                            
    178         except exceptions.ExtractError:
    179             # To force an update to the js file, we clear the cache and retry                                                                           

~/anaconda3/lib/python3.8/site-packages/pytube/extract.py in apply_signature(stream_manifest, vid_info, js)                                                     
    407 
    408     """
--> 409     cipher = Cipher(js=js)
    410 
    411     for i, stream in enumerate(stream_manifest):

~/anaconda3/lib/python3.8/site-packages/pytube/cipher.py in __init__(self, js)
     42 
     43         self.throttling_plan = get_throttling_plan(js)
---> 44         self.throttling_array = get_throttling_function_array(js)
     45 
     46         self.calculated_n = None

~/anaconda3/lib/python3.8/site-packages/pytube/cipher.py in get_throttling_function_array(js)                                                                   
    321 
    322     array_raw = find_object_from_startpoint(raw_code, match.span()[1] - 1)                                                                              
--> 323     str_array = throttling_array_split(array_raw)
    324 
    325     converted_array = []

~/anaconda3/lib/python3.8/site-packages/pytube/parser.py in throttling_array_split(js_array)                                                                    
    156             # Handle functions separately. These can contain commas
    157             match = func_regex.search(curr_substring)
--> 158             match_start, match_end = match.span()
    159 
    160             function_text = find_object_from_startpoint(curr_substring, match.span()[1])

AttributeError: 'NoneType' object has no attribute 'span'

and I wonder why? Can anyone help me? I am running this snippet in an ipython console (IPython version 7.22.0) with Python 3.8.8 in a conda environment.

2
  • 2
    Honestly, this might have been broken by changes with YouTube. Just speculation though. I get the exact same issue. Nov 22, 2021 at 2:30
  • Hey, I just got this problem today, too ...
    – Yi-An
    Nov 22, 2021 at 8:48

6 Answers 6

17

Found this issue, pytube v11.0.1. It's a little late for me, but if no one has submitted a fix tomorrow I'll check it out.

in C:\Python38\lib\site-packages\pytube\parser.py

Change this line:

152: func_regex = re.compile(r"function\([^)]+\)")

to this:

152: func_regex = re.compile(r"function\([^)]?\)")

The issue is that the regex expects a function with an argument, but I guess youtube added some src that includes non-paramterized functions.

5
  • I am too lazy but I guess this should be reported on github to inform the developers about it. Nov 22, 2021 at 13:07
  • Thanks a lot, worked fine. Nov 22, 2021 at 18:48
  • 1
    actually, even better would be function\([^)]*?\)
    – Braynstorm
    Nov 23, 2021 at 14:04
  • 1
    It was working yesterday, now it's not working again, at least on my side, I checked line 152, it's okay, however, it's again throwing me the same error... Nov 23, 2021 at 19:07
  • Yalchin, I just had this issue again after having fixed it this way as well. I put it back to how it was originally and it is now working again. I suspect it was an interim change at youtube.
    – Matt
    Jan 6, 2022 at 22:06
15

Edit:

I have tried to keep my solution up to date but I started to feel that pytube appears to have problems frequently and these bugs detrimentally affect my project. So I refactored it and started to use ytdlp library from now on. So at the time you are reading this, this solution might not work for you.

For the time being as we got this error again. You can try solution given here

Or

Make sure you in pytube/cipher.py on line 293:

You change

name = re.escape(get_throttling_function_name(js))

to

name = "hha"

Thanks Jeffery Williams

4
  • 4
    After applying the parser.py avobe mention patches with no success, this worked for me. Finger cross and see how long it lasts!
    – sscalvo
    Feb 1, 2022 at 11:08
  • 1
    none of the other answers worked, but this one does
    – May
    Feb 4, 2022 at 0:31
  • For me, nothing worked except a fresh uninstall-installation of pytube.
    – meW
    Feb 24, 2022 at 12:18
  • Solution above is outdated as I mentioned in the edit. Feb 25, 2022 at 9:48
9

I had the same problem, I changed parser.py as in the answer above, just forked pytube lib on GitHub, and changed the file.

You can install pytube this way:

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

Instead of:

pip install pytube
3
  • There is another error since yesterday. Any idea how to fix it?
    – wmcnally
    Nov 24, 2021 at 12:40
  • I have no idea, because i didn't get any errors
    – baxgf
    Nov 25, 2021 at 16:21
  • this is working for me as of 13-02-2022 ! Feb 13, 2022 at 14:59
5

The problematic version was 11.0.1 and now has already been fixed, so you just need to upgrade to the newer version, and will be working fine again:

pip install --upgrade pytube
0

The most simplest solution can be:

  1. Go to the location where your python is installed (Mainly in : C:/Users/HP/appdata/local/programs/python)
  2. Search for pytube in the top right corner and delete all the pytube modules you see
  3. open your cmd (in administrative mode) here
  4. enter the command : pip install pytube
  5. it's done ✅ Now try running the code again [ ⭐I had the same issue and these steps solved it]
0

use yt-dlp insted of pytube, it works fine

   pip install yt-dlp

Not the answer you're looking for? Browse other questions tagged or ask your own question.