我正在尝试执行以下代码,即从youtube url下载音频文件
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)但是我得到了下面的回溯
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知道怎么解决这个问题吗?
发布于 2022-05-06 20:43:06
解决问题的步骤:首先,使用代码检测包pytube路径
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))),
https://stackoverflow.com/questions/72145556
复制相似问题