Pytube给出了各种正则错误错误

发布于 2025-01-20 21:30:17 字数 5074 浏览 3 评论 0原文

我有一个用 python 下载 youtube 视频的程序。该程序是:

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.")

我尝试下载视频(https://www.youtube.com/watch? v=jikcB7_gj8A),它给了我这个错误:

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}).*

所以我尝试了解决方案 这里(将cipher.py中的正则表达式替换为'r'\bc\s*&&\s*d\.set\([^,]+\s*,\s*\([^)]*\)\s*\( \s*(?P[a-zA-Z0-9$]+)\(''),现在我收到此错误:

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=\.\(\"\)]*;(.*);(?:.+)} 

我不知所措。编辑

:我正在运行 pytube版本 12.0.0,从 https://github.com/nficano/pytube 下载。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪漫之都 2025-01-27 21:30:17

在cypher.py中修改第273行并更改正则表达式。
注意:{3} 现在是 {2}。这应该至少在它再次改变之前有效。

第273行:r'([az]\s*=\s*([a-zA-Z0-9$]{2})([\d+])?([az])',

同时编辑第288

行288: nfunc=re.escape(function_match.group(1))),

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))),

过气美图社 2025-01-27 21:30:17

我重新编写了您的代码,这在我的系统上没有引发任何错误。

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

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
三人与歌 2025-01-27 21:30:17

显然没有提供解决方案,但供您参考。(我无法评论,我的声誉不够。)

自 4 月 13 日以来,我已经看到这个问题出现,有几个用户,我的设备上也有同样的问题。直到 4 月 13 日,一切都很好。

您的代码或 pytube 本身没有任何问题。 YouTube 改变了模式,因此 pytube 中运行的旧模式不再适用于 URL。

有相当多的修复,例如您尝试过的修复,以及使用 cipher.py 文件、更改函数模式以及使用 git 版本,但没有一个对我有用。
因此,唯一的修复方法是检查是否有更多修复程序(如果有更多修复程序可用)或等待 pytube 更新以进行更改。还有其他应用程序,例如 pytube、yt-dlp 和 Youtube-dl。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文