'Pytube is giving assorted regex errors
I have a program to download youtube videos with python. The program is:
from pytube import YouTube
def Download(video_list):
for url in video_list:
print("url: [%s]" % url)
youtube = YouTube(url)
youtube.streams.get_highest_resolution().download("C:/Users/user1/Downloads")
print (f'{youtube.title} downloaded.')
video_list = []
run = True
while run == True:
link = str(input("Enter youtube URL, or press D to download: "))
if link.find("youtu") != 1:
video_list.append(link)
if link == 'd' or link == 'D':
Download(video_list)
run = False
elif link.find("youtu") == -1:
print ("Invalid youtube URL.")
I tried downloading a video (https://www.youtube.com/watch?v=jikcB7_gj8A), and it gave me this error:
url: [https://www.youtube.com/watch?v=jikcB7_gj8A]
Traceback (most recent call last):
File "c:\Users\user1\Desktop\Mostly Python\youtube_downloader.py", line 27, in <module>
File "c:\Users\user1\Desktop\Mostly Python\youtube_downloader.py", line 8, in Download
youtube = YouTube(url)
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\__main__.py", line 71, in __init__
self.video_id = extract.video_id(url)
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\extract.py", line 133, in video_id
return regex_search(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", url, group=1)
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\helpers.py", line 129, in regex_search
raise RegexMatchError(caller="regex_search", pattern=pattern)
pytube.exceptions.RegexMatchError: regex_search: could not find match for (?:v=|\/)([0-9A-Za-z_-]{11}).*
So I tried the solution here (replace the regexes in cipher.py with 'r'\bc\s*&&\s*d\.set\([^,]+\s*,\s*\([^)]*\)\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(''), and now I'm getting this error:
url: [https://www.youtube.com/watch?v=jikcB7_gj8A]
Traceback (most recent call last):
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\__main__.py", line 181, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\cipher.py", line 29, in __init__
self.transform_plan: List[str] = get_transform_plan(js)
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\cipher.py", line 186, in get_transform_plan
return regex_search(pattern, js, group=1).split(";")
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\helpers.py", line 129, in regex_search
raise RegexMatchError(caller="regex_search", pattern=pattern)
pytube.exceptions.RegexMatchError: regex_search: could not find match for qra\[0\]=function\(\w\){[a-z=\.\(\"\)]*;(.*);(?:.+)}
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\user1\Desktop\Mostly Python\youtube_downloader.py", line 27, in <module>
Download(video_list)
File "c:\Users\user1\Desktop\Mostly Python\youtube_downloader.py", line 9, in Download
youtube.streams.get_highest_resolution().download("C:/Users/user1/Downloads")
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\__main__.py", line 296, in streams
return StreamQuery(self.fmt_streams)
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\__main__.py", line 188, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\cipher.py", line 29, in __init__
self.transform_plan: List[str] = get_transform_plan(js)
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\cipher.py", line 186, in get_transform_plan
return regex_search(pattern, js, group=1).split(";")
File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\pytube\helpers.py", line 129, in regex_search
raise RegexMatchError(caller="regex_search", pattern=pattern)
pytube.exceptions.RegexMatchError: regex_search: could not find match for qra\[0\]=function\(\w\){[a-z=\.\(\"\)]*;(.*);(?:.+)}
I'm at a loss. What do I do?
Edit: I am running pytube version 12.0.0, downloaded from https://github.com/nficano/pytube.
Solution 1:[1]
I reworked your code, which throws no errors on my system.
import tldextract
from pytube import YouTube
def Download(video_list):
for url in video_list:
youtube = YouTube(url.strip())
youtube.streams.get_highest_resolution().download()
print(f'The video - {youtube.title} - was downloaded.')
video_list = []
run = True
while run == True:
link = str(input("Enter a YouTube URL or press D to download the video(s): "))
domain_name = tldextract.extract(link).domain
if link.lower() == 'd':
Download(video_list)
run = False
elif domain_name == 'youtube':
video_list.append(link)
else:
print("Invalid youtube URL.")
Enter a YouTube URL or press D to download the video(s): https://www.youtube.com/watch?v=jikcB7_gj8A
Enter a YouTube URL or press D to download the video(s): D
The video - How Teachers Help You During Tests #Shorts - was downloaded.
Process finished with exit code 0
Solution 2:[2]
In cypher.py Modify line 273 and change the regular expression. Note: {3} is now {2}. This should work at least until it changes again.
Line 273: r'([a-z]\s*=\s*([a-zA-Z0-9$]{2})([\d+])?([a-z])',
also edit line 288
Line 288: nfunc=re.escape(function_match.group(1))),
Solution 3:[3]
Apparently providing no solution, but for your information.(i cant comment, I am not reputed enough.)
I've seen this issue arising since April 13th with, several users and same on my device. It worked fine until April 13th.
This’s not anything wrong with your code or pytube itself. YouTube changed patterns on their end, so older patterns functioning in pytube no longer works for the URLs.
There were quite a few fixes like the one you’ve tried and with cipher.py file, changing function patterns, and using git versions, but non of them worked for me yet. So only fix is to check for more fixes if any more is available or wait for pytube to update for the change. There are other applications like pytube, yt-dlp and Youtube-dl.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Tharushi |
