pytube: AttributeError: 'NoneType' object has no attribute 'span'
Question:
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.
Answers:
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:Python38libsite-packagespytubeparser.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.
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
The most simplest solution can be:
- Go to the location where your python is installed
(Mainly in : C:/Users/HP/appdata/local/programs/python)
- Search for pytube in the top right corner and delete all the pytube modules you see
- open your cmd (in administrative mode) here
- enter the command :
pip install pytube
- it’s done ✅
Now try running the code again
[ ⭐I had the same issue and these steps solved it]
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
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
use yt-dlp insted of pytube, it works fine
pip install yt-dlp
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.
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:Python38libsite-packagespytubeparser.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.
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
The most simplest solution can be:
- Go to the location where your python is installed
(Mainly in : C:/Users/HP/appdata/local/programs/python) - Search for pytube in the top right corner and delete all the pytube modules you see
- open your cmd (in administrative mode) here
- enter the command :
pip install pytube - it’s done ✅
Now try running the code again
[ ⭐I had the same issue and these steps solved it]
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
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
use yt-dlp insted of pytube, it works fine
pip install yt-dlp