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

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

why I get error-runtime with this python youtubedownloader code

问题

以下是代码的翻译部分:

  1. from pytube import YouTube
  2. # 从用户获取视频链接
  3. link = input("输入要下载的YouTube视频链接: ")
  4. yt = YouTube(link)
  5. # 显示视频详情
  6. print("标题: ", yt.title)
  7. print("观看次数: ", yt.views)
  8. print("视频长度: ", yt.length)
  9. print("视频评分: ", yt.rating)
  10. # 获取最高分辨率的视频流
  11. ys = yt.streams.get_highest_resolution()
  12. # 开始下载
  13. print("下载中...")
  14. ys.download()
  15. 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

  1. from pytube import YouTube
  2. #ask for the link from user
  3. link = input("Enter the link of YouTube video you want to download: ")
  4. yt = YouTube(link)
  5. #Showing details
  6. print("Title: ",yt.title)
  7. print("Number of views: ",yt.views)
  8. print("Length of video: ",yt.length)
  9. print("Rating of video: ",yt.rating)
  10. #Getting the highest resolution possible
  11. ys = yt.streams.get_highest_resolution()
  12. #Starting download
  13. print("Downloading...")
  14. ys.download()
  15. print("Download completed!!")

I'm getting this
ERROR

  1. $ python dl.py
  2. Enter the link of YouTube video you want to download: https://www.youtube.com/watch?v=_oHTzIsLMGc
  3. Title: CAER EN EL SUEÑO PROFUNDO,Sanación del Estrés,Ansiedad y Estados Depresivos,Restauración Corporal#16
  4. Number of views: 34789
  5. Length of video: 367200
  6. Rating of video: None
  7. Traceback (most recent call last):
  8. File "/usr/local/lib/python3.9/site-packages/pytube/__main__.py", line 181, in fmt_streams
  9. extract.apply_signature(stream_manifest, self.vid_info, self.js)
  10. File "/usr/local/lib/python3.9/site-packages/pytube/extract.py", line 409, in apply_signature
  11. cipher = Cipher(js=js)
  12. File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 43, in __init__
  13. self.throttling_plan = get_throttling_plan(js)
  14. File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 405, in get_throttling_plan
  15. raw_code = get_throttling_function_code(js)
  16. File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 311, in get_throttling_function_code
  17. name = re.escape(get_throttling_function_name(js))
  18. File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 296, in get_throttling_function_name
  19. raise RegexMatchError(
  20. pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple
  21. During handling of the above exception, another exception occurred:
  22. Traceback (most recent call last):
  23. File "/cygdrive/c/Users/hynek0/Desktop/FU/VILLA/dl.py", line 13, in <module>
  24. ys = yt.streams.get_highest_resolution()
  25. File "/usr/local/lib/python3.9/site-packages/pytube/__main__.py", line 296, in streams
  26. return StreamQuery(self.fmt_streams)
  27. File "/usr/local/lib/python3.9/site-packages/pytube/__main__.py", line 188, in fmt_streams
  28. extract.apply_signature(stream_manifest, self.vid_info, self.js)
  29. File "/usr/local/lib/python3.9/site-packages/pytube/extract.py", line 409, in apply_signature
  30. cipher = Cipher(js=js)
  31. File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 43, in __init__
  32. self.throttling_plan = get_throttling_plan(js)
  33. File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 405, in get_throttling_plan
  34. raw_code = get_throttling_function_code(js)
  35. File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 311, in get_throttling_function_code
  36. name = re.escape(get_throttling_function_name(js))
  37. File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 296, in get_throttling_function_name
  38. raise RegexMatchError(
  39. pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple

EDIT line 264

  1. $ pytube https://www.youtube.com/watch?v=_oHTzIsLMGc
  2. Loading video...
  3. Traceback (most recent call last):
  4. File "/usr/local/bin/pytube", line 8, in <module>
  5. sys.exit(main())
  6. File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 53, in main
  7. _perform_args_on_youtube(youtube, args)
  8. File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 60, in _perform_args_on_youtube
  9. download_highest_resolution_progressive(
  10. File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 479, in download_highest_resolution_progressive
  11. _download(stream, target=target)
  12. File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 256, in _download
  13. filesize_megabytes = stream.filesize // 1048576
  14. File "/usr/local/lib/python3.9/site-packages/pytube/streams.py", line 157, in filesize
  15. self._filesize = request.filesize(self.url)
  16. File "/usr/local/lib/python3.9/site-packages/pytube/request.py", line 204, in filesize
  17. return int(head(url)["content-length"])
  18. File "/usr/local/lib/python3.9/site-packages/pytube/request.py", line 268, in head
  19. response_headers = _execute_request(url, method="HEAD").info()
  20. File "/usr/local/lib/python3.9/site-packages/pytube/request.py", line 37, in _execute_request
  21. return urlopen(request, timeout=timeout) # nosec
  22. File "/usr/lib/python3.9/urllib/request.py", line 214, in urlopen
  23. return opener.open(url, data, timeout)
  24. File "/usr/lib/python3.9/urllib/request.py", line 523, in open
  25. response = meth(req, response)
  26. File "/usr/lib/python3.9/urllib/request.py", line 632, in http_response
  27. response = self.parent.error(
  28. File "/usr/lib/python3.9/urllib/request.py", line 561, in error
  29. return self._call_chain(*args)
  30. File "/usr/lib/python3.9/urllib/request.py", line 494, in _call_chain
  31. result = func(*args)
  32. File "/usr/lib/python3.9/urllib/request.py", line 641, in http_error_default
  33. raise HTTPError(req.full_url, code, msg, hdrs, fp)
  34. urllib.error.HTTPError: HTTP Error 500: Internal Server Error

答案1

得分: 1

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

  1. from pytube import YouTube, Playlist
  2. # 下载单个视频的函数
  3. def download_video(url):
  4. try:
  5. yt = YouTube(url)
  6. stream = yt.streams.get_highest_resolution()
  7. print(f"下载视频: {yt.title}")
  8. stream.download()
  9. print("下载完成!")
  10. except Exception as e:
  11. print(f"错误: {str(e)}")
  12. # 下载播放列表的函数
  13. def download_playlist(url):
  14. try:
  15. playlist = Playlist(url)
  16. print(f"播放列表中的总视频数: {len(playlist.video_urls)}")
  17. for video_url in playlist.video_urls:
  18. download_video(video_url)
  19. except Exception as e:
  20. print(f"错误: {str(e)}")
  21. # 主程序
  22. user_input = input("输入 YouTube 视频 URL 或播放列表 URL: ")
  23. if "youtube.com/playlist" in user_input:
  24. download_playlist(user_input)
  25. else:
  26. download_video(user_input)

输出:

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

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

  1. from pytube import YouTube, Playlist
  2. # Function to download a single video
  3. def download_video(url):
  4. try:
  5. yt = YouTube(url)
  6. stream = yt.streams.get_highest_resolution()
  7. print(f"Downloading video: {yt.title}")
  8. stream.download()
  9. print("Download completed!")
  10. except Exception as e:
  11. print(f"Error: {str(e)}")
  12. # Function to download a playlist
  13. def download_playlist(url):
  14. try:
  15. playlist = Playlist(url)
  16. print(f"Total videos in playlist: {len(playlist.video_urls)}")
  17. for video_url in playlist.video_urls:
  18. download_video(video_url)
  19. except Exception as e:
  20. print(f"Error: {str(e)}")
  21. # Main program
  22. user_input = input("Enter YouTube video URL or playlist URL: ")
  23. if "youtube.com/playlist" in user_input:
  24. download_playlist(user_input)
  25. else:
  26. download_video(user_input)

output:

  1. PS D:\pgm\python\pratice> & C:/Python/Python_3.11.3/python.exe d:/pgm/python/pratice/test2.py
  2. Enter YouTube video URL or playlist URL: https://youtu.be/WuAAWQshbTM
  3. Downloading video: How to Do 15 PULLUPS OR MORE in a Row (GUARANTEED!)
  4. Download completed!
  5. PS D:\pgm\python\pratice> & C:/Python/Python_3.11.3/python.exe d:/pgm/python/pratice/test2.py
  6. Enter YouTube video URL or playlist URL: https://youtube.com/playlist?list=PLjqueTFOWIlKOKqeRMB7K-64iGresYwCn
  7. Total videos in playlist: 2
  8. Downloading video: 30 Minute Full Body Dumbbell HIIT Workout [ADVANCED]
  9. Download completed!
  10. Downloading video: Ultimate Full-Body Dumbbell Workout | Andy Speer
  11. 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为以下内容:
  1. function_patterns = [
  2. # https://github.com/ytdl-org/youtube-dl/issues/29326#issuecomment-865985377
  3. # https://github.com/yt-dlp/yt-dlp/commit/48416bc4a8f1d5ff07d5977659cb8ece7640dcd8
  4. # var Bpa = [iha];
  5. # ...
  6. # a.C && (b = a.get("n")) && (b = Bpa[0](b), a.set("n", b),
  7. # Bpa.length || iha("")) }};
  8. # 在上述情况中,`iha`是相关的函数名称
  9. r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&.*?\|\|\s*([a-z]+)',
  10. r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)',
  11. ]

在我的情况下,这是第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

  1. function_patterns = [
  2. # https://github.com/ytdl-org/youtube-dl/issues/29326#issuecomment-865985377
  3. # https://github.com/yt-dlp/yt-dlp/commit/48416bc4a8f1d5ff07d5977659cb8ece7640dcd8
  4. # var Bpa = [iha];
  5. # ...
  6. # a.C && (b = a.get("n")) && (b = Bpa[0](b), a.set("n", b),
  7. # Bpa.length || iha("")) }};
  8. # In the above case, `iha` is the relevant function name
  9. r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&.*?\|\|\s*([a-z]+)',
  10. 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:

确定