5

我正在尝试使用 Python pytube3 制作 YouTube 视频下载器,但它不会下载所有视频。有些视频很容易下载,但有些视频不会下载,而是显示错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 297, in apply_descrambler
    for format_item in formats
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 297, in <listcomp>
    for format_item in formats
KeyError: 'url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\tarun\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/tarun/PycharmProjects/YTDownloader/YTD.py", line 15, in video_download
    my_video = YouTube(input_user)
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\__main__.py", line 92, in __init__
    self.descramble()
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\__main__.py", line 132, in descramble
    apply_descrambler(self.player_config_args, fmt)
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 301, in apply_descrambler
    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 301, in <listcomp>
    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
KeyError: 'cipher'
4

5 回答 5

12

这是pytube文件 extract.py 中的错误。

  1. 转到安装包的位置。如果你不知道在哪里,运行命令

    pip show pytube3
    

    它会给你这样的东西:

pip 显示包

我们可以看到Location: c:\users\tiago\anaconda3\lib\site-packages

  1. 转到该位置,打开文件夹 pytube 和文件 extract.py

pythube 位置

  1. 在文件中,行号。306 或 301,你会发现parse_qs(formats[i]["cipher"]). 如果是,则更"cipher"改为"signatureCipher"(确保“C”是大写字母)。

    所以,你最初会有

     cipher_url = [
                     parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
                 ]
    

    但应该是

     cipher_url = [
                     parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)
                 ]
    

pytube 提取密码错误已修复

  1. 运行以下脚本以查看它是否正常工作

     # -*- coding: utf-8 -*-
     """
     Created on Mon Jun 15 12:21:49 2020
    
     @author: tiago
     """
     from pytube import YouTube
    
     video_url = "https://youtu.be/gp5tziO5lXg" # YouTube video URL
     youtube = YouTube(video_url)
     video = youtube.streams.first()
     video.download("C:/Users/tiago/Desktop/videos/") # Path where to store the video
    

然后,您将在该文件夹中看到下载的视频

如何用 Python 从 YouTube 下载视频

于 2020-05-31T12:04:27.133 回答
1
  1. 只需转到pytube\extract.py(在pytube库中)文件。文件的路径将是(在Windows中):C:\ProgramData\Anaconda3\lib\site-packages\pytube\extract.py

  2. 打开extract.py文件并搜索行:

    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)

  3. 现在将 ' cipher' 替换为 ' signatureCipher'。

  4. 保存。

  5. 现在再次运行您的代码

于 2020-07-21T08:59:01.430 回答
0

这是 pytube3 的问题,我相信到目前为止他们还没有提交修复。 这是github上问题的链接

于 2020-05-30T16:18:55.060 回答
0

如果您收到错误为 Keyerror:"cipher",然后转到 pytube 打开 extract.py 的位置,在第 301 行,您将得到这个

cipher_url = [
                parse_qs(formats[i]["Cipher"]) for i, data in enumerate(formats)
            ]

现在将此行编辑为此

cipher_url = [
                parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)
            ]

保存更改并....繁荣你完成了。现在尝试下载视频,您现在不会收到任何错误。

于 2020-07-17T09:10:18.297 回答
-1

在 pytube 库中,此文件中有一个 extract.py 文件,将密码更改为签名密码

于 2021-04-19T11:02:53.927 回答