为什么我在使用这个Python的YouTube下载器代码时会收到运行时错误?

huangapple go评论87阅读模式
英文:

why I get error-runtime with this python youtubedownloader code

问题

以下是代码的翻译部分:

from pytube import YouTube

# 从用户获取视频链接
link = input("输入要下载的YouTube视频链接: ")
yt = YouTube(link)

# 显示视频详情
print("标题: ", yt.title)
print("观看次数: ", yt.views)
print("视频长度: ", yt.length)
print("视频评分: ", yt.rating)

# 获取最高分辨率的视频流
ys = yt.streams.get_highest_resolution()

# 开始下载
print("下载中...")
ys.download()
print("下载完成!!")

请注意,你的错误似乎与YouTube的一些更改或限制有关,可能需要检查你的网络连接或确保你的代码库(pytube)是最新的。此外,你的代码中存在一些HTML实体字符("),你可以将它们替换为普通的引号字符(")以避免问题。

英文:

How can I download video from YouTube is there some MWE??
I want to download a playlist from YouTube site:
(I want to download a video from YouTube as follows)
https://www.youtube.com/watch?v=_oHTzIsLMGc

With this code

from pytube import YouTube

#ask for the link from user
link = input("Enter the link of YouTube video you want to download:  ")
yt = YouTube(link)

#Showing details
print("Title: ",yt.title)
print("Number of views: ",yt.views)
print("Length of video: ",yt.length)
print("Rating of video: ",yt.rating)
#Getting the highest resolution possible
ys = yt.streams.get_highest_resolution()

#Starting download
print("Downloading...")
ys.download()
print("Download completed!!")

I'm getting this
ERROR

$ python dl.py
Enter the link of YouTube video you want to download:  https://www.youtube.com/watch?v=_oHTzIsLMGc
Title:  CAER EN EL SUEÑO PROFUNDO,Sanación del Estrés,Ansiedad y Estados Depresivos,Restauración Corporal#16
Number of views:  34789
Length of video:  367200
Rating of video:  None
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pytube/__main__.py", line 181, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "/usr/local/lib/python3.9/site-packages/pytube/extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 43, in __init__
    self.throttling_plan = get_throttling_plan(js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 405, in get_throttling_plan
    raw_code = get_throttling_function_code(js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 311, in get_throttling_function_code
    name = re.escape(get_throttling_function_name(js))
  File "/usr/local/lib/python3.9/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 "/cygdrive/c/Users/hynek0/Desktop/FU/VILLA/dl.py", line 13, in <module>
    ys = yt.streams.get_highest_resolution()
  File "/usr/local/lib/python3.9/site-packages/pytube/__main__.py", line 296, in streams
    return StreamQuery(self.fmt_streams)
  File "/usr/local/lib/python3.9/site-packages/pytube/__main__.py", line 188, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "/usr/local/lib/python3.9/site-packages/pytube/extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 43, in __init__
    self.throttling_plan = get_throttling_plan(js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 405, in get_throttling_plan
    raw_code = get_throttling_function_code(js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 311, in get_throttling_function_code
    name = re.escape(get_throttling_function_name(js))
  File "/usr/local/lib/python3.9/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

EDIT line 264

$ pytube https://www.youtube.com/watch?v=_oHTzIsLMGc
Loading video...
Traceback (most recent call last):
  File "/usr/local/bin/pytube", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 53, in main
    _perform_args_on_youtube(youtube, args)
  File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 60, in _perform_args_on_youtube
    download_highest_resolution_progressive(
  File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 479, in download_highest_resolution_progressive
    _download(stream, target=target)
  File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 256, in _download
    filesize_megabytes = stream.filesize // 1048576
  File "/usr/local/lib/python3.9/site-packages/pytube/streams.py", line 157, in filesize
    self._filesize = request.filesize(self.url)
  File "/usr/local/lib/python3.9/site-packages/pytube/request.py", line 204, in filesize
    return int(head(url)["content-length"])
  File "/usr/local/lib/python3.9/site-packages/pytube/request.py", line 268, in head
    response_headers = _execute_request(url, method="HEAD").info()
  File "/usr/local/lib/python3.9/site-packages/pytube/request.py", line 37, in _execute_request
    return urlopen(request, timeout=timeout)  # nosec
  File "/usr/lib/python3.9/urllib/request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.9/urllib/request.py", line 523, in open
    response = meth(req, response)
  File "/usr/lib/python3.9/urllib/request.py", line 632, in http_response
    response = self.parent.error(
  File "/usr/lib/python3.9/urllib/request.py", line 561, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.9/urllib/request.py", line 494, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.9/urllib/request.py", line 641, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 500: Internal Server Error

答案1

得分: 1

这是我的代码来下载视频或播放列表:

from pytube import YouTube, Playlist

# 下载单个视频的函数
def download_video(url):
    try:
        yt = YouTube(url)
        stream = yt.streams.get_highest_resolution()
        print(f"下载视频: {yt.title}")
        stream.download()
        print("下载完成!")
    except Exception as e:
        print(f"错误: {str(e)}")

# 下载播放列表的函数
def download_playlist(url):
    try:
        playlist = Playlist(url)
        print(f"播放列表中的总视频数: {len(playlist.video_urls)}")
        for video_url in playlist.video_urls:
            download_video(video_url)
    except Exception as e:
        print(f"错误: {str(e)}")

# 主程序
user_input = input("输入 YouTube 视频 URL 或播放列表 URL: ")
if "youtube.com/playlist" in user_input:
    download_playlist(user_input)
else:
    download_video(user_input)

输出:

PS D:\pgm\python\pratice> & C:/Python/Python_3.11.3/python.exe d:/pgm/python/pratice/test2.py
输入 YouTube 视频 URL 或播放列表 URL: https://youtu.be/WuAAWQshbTM
下载视频: How to Do 15 PULLUPS OR MORE in a Row (GUARANTEED!)
下载完成!
PS D:\pgm\python\pratice> & C:/Python/Python_3.11.3/python.exe d:/pgm/python/pratice/test2.py
输入 YouTube 视频 URL 或播放列表 URL: https://youtube.com/playlist?list=PLjqueTFOWIlKOKqeRMB7K-64iGresYwCn
播放列表中的总视频数: 2
下载视频: 30 Minute Full Body Dumbbell HIIT Workout [ADVANCED]
下载完成!
下载视频: Ultimate Full-Body Dumbbell Workout | Andy Speer
下载完成!
英文:

here is my code to download a video or a playlist:

from pytube import YouTube, Playlist

# Function to download a single video
def download_video(url):
    try:
        yt = YouTube(url)
        stream = yt.streams.get_highest_resolution()
        print(f"Downloading video: {yt.title}")
        stream.download()
        print("Download completed!")
    except Exception as e:
        print(f"Error: {str(e)}")

# Function to download a playlist
def download_playlist(url):
    try:
        playlist = Playlist(url)
        print(f"Total videos in playlist: {len(playlist.video_urls)}")
        for video_url in playlist.video_urls:
            download_video(video_url)
    except Exception as e:
        print(f"Error: {str(e)}")

# Main program
user_input = input("Enter YouTube video URL or playlist URL: ")
if "youtube.com/playlist" in user_input:
    download_playlist(user_input)
else:
    download_video(user_input)

output:

PS D:\pgm\python\pratice> & C:/Python/Python_3.11.3/python.exe d:/pgm/python/pratice/test2.py
Enter YouTube video URL or playlist URL: https://youtu.be/WuAAWQshbTM
Downloading video: How to Do 15 PULLUPS OR MORE in a Row (GUARANTEED!)
Download completed!
PS D:\pgm\python\pratice> & C:/Python/Python_3.11.3/python.exe d:/pgm/python/pratice/test2.py
Enter YouTube video URL or playlist URL: https://youtube.com/playlist?list=PLjqueTFOWIlKOKqeRMB7K-64iGresYwCn
Total videos in playlist: 2
Downloading video: 30 Minute Full Body Dumbbell HIIT Workout [ADVANCED]
Download completed!
Downloading video: Ultimate Full-Body Dumbbell Workout | Andy Speer
Download completed!

答案2

得分: 0

这似乎是pytube中的一个BUG。在存储库中已经提出了一个问题(RegexMatchError: get_throttling_function_name: 无法找到多个#1707的匹配项1)。查看这个评论以获取一种解决方法。

英文:

This seems to be a BUG in pytube. A issue has already raised RegexMatchError: get_throttling_function_name: could not find match for multiple #1707) in the repository. See this comment for a workaround soultion.

答案3

得分: 0

解决方案:

  1. 前往pytube库(使用pip show pytube查找位置)
  2. 更改cipher.py -> get_throttling_function_name -> function_patterns为以下内容:
function_patterns = [
    # https://github.com/ytdl-org/youtube-dl/issues/29326#issuecomment-865985377
    # https://github.com/yt-dlp/yt-dlp/commit/48416bc4a8f1d5ff07d5977659cb8ece7640dcd8
    # var Bpa = [iha];
    # ...
    # a.C && (b = a.get("n")) && (b = Bpa[0](b), a.set("n", b),
    # Bpa.length || iha("")) }};
    # 在上述情况中,`iha`是相关的函数名称
    r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&.*?\|\|\s*([a-z]+)',
    r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)',
]

在我的情况下,这是第264行
4. 运行py脚本
这对我有用

英文:

Solution:

  1. go to the pytube library (you find the location using pip show pytube )
    为什么我在使用这个Python的YouTube下载器代码时会收到运行时错误?

  2. change cipher.py -> get_throttling_function_name -> function_patterns to

function_patterns = [
    # https://github.com/ytdl-org/youtube-dl/issues/29326#issuecomment-865985377
    # https://github.com/yt-dlp/yt-dlp/commit/48416bc4a8f1d5ff07d5977659cb8ece7640dcd8
    # var Bpa = [iha];
    # ...
    # a.C && (b = a.get("n")) && (b = Bpa[0](b), a.set("n", b),
    # Bpa.length || iha("")) }};
    # In the above case, `iha` is the relevant function name
    r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&.*?\|\|\s*([a-z]+)',
    r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)', ]

in my case it is 264 line
为什么我在使用这个Python的YouTube下载器代码时会收到运行时错误?
4. run the py script<br/>
this worked for me

huangapple
  • 本文由 发表于 2023年7月10日 21:15:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定