首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅下载音频youtube引发正则匹配错误

仅下载音频youtube引发正则匹配错误
EN

Stack Overflow用户
提问于 2022-05-06 17:49:35
回答 1查看 44关注 0票数 0

我正在尝试执行以下代码,即从youtube url下载音频文件

代码语言:javascript
复制
from pathlib import Path
from pytube import YouTube

def download_youtube_video(youtube_url, output_path):
    audio_file = YouTube(youtube_url).streams.get_audio_only().download(output_path=output_path)
    audio_file = Path(audio_file)
    audio_file = audio_file.replace(audio_file.with_suffix(".mp3"))

    return audio_file

youtube_url = 'https://youtu.be/_H5hsUwv8lE'
output_path = Path(__file__).parent
audio_file = download_youtube_video(youtube_url, output_path)

但是我得到了下面的回溯

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 181, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 43, in __init__
    self.throttling_plan = get_throttling_plan(js)
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 405, in get_throttling_plan
    raw_code = get_throttling_function_code(js)
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 311, in get_throttling_function_code
    name = re.escape(get_throttling_function_name(js))
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 296, in get_throttling_function_name
    raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "demo.py", line 18, in <module>
    audio_file = download_youtube_video(youtube_url, output_path)
  File "demo.py", line 6, in download_youtube_video
    audio_file = YouTube(youtube_url).streams.get_audio_only().download(output_path=output_path)
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 296, in streams
    return StreamQuery(self.fmt_streams)
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 188, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 43, in __init__
    self.throttling_plan = get_throttling_plan(js)
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 405, in get_throttling_plan
    raw_code = get_throttling_function_code(js)
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 311, in get_throttling_function_code
    name = re.escape(get_throttling_function_name(js))
  File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 296, in get_throttling_function_name
    raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple

知道怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2022-05-06 20:43:06

解决问题的步骤:首先,使用代码检测包pytube路径

代码语言:javascript
复制
import pytube
import os

print(pytube.__file__)
print(os.path.dirname(pytube.__file__))

导航到目录pytube并修改cipher.py,第273行r'\([a-z]\s*=\s*([a-zA-Z0-9$]{3})(\[\d+\])?\([a-z]\)',改为r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)',

第288行nfunc=function_match.group(1)),改为nfunc=re.escape(function_match.group(1))),

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72145556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档