gpt4 book ai didi

python - 如何使用 Python 下载 YouTube 视频?

转载 作者:行者123 更新时间:2023-12-04 15:56:51 37 4
gpt4 key购买 nike

关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

4 个月前关闭。




Improve this question




我想用 Python 下载 YouTube 视频。我用python试过opencv,但是一直有问题。我正在尝试使用 pytube3 和 Python 3。我尝试了这个程序(我在 StackOverflow 上找到的):

from pytube import YouTube
YouTube('http://youtube.com/watch?v=9bZkp7q19f0').streams.first().download()
但它因“HTTP 错误 410”错误而失败。当我运行各种 pytube 程序时,我不断收到“HTTP 错误 410”。我更改了 URL 并在浏览器中测试了 URL。当我运行 Python 程序下载视频时,没有任何效果。
我正在尝试运行这个程序(正如我发现的那样 here):
from pytube import YouTube

try:
yt_obj = YouTube('https://www.youtube.com/watch?v=DkU9WFj8sYo')

print(f'Video Title is {yt_obj.title}')
print(f'Video Length is {yt_obj.length} seconds')
print(f'Video Description is {yt_obj.description}')
print(f'Video Rating is {yt_obj.rating}')
print(f'Video Views Count is {yt_obj.views}')
print(f'Video Author is {yt_obj.author}')

except Exception as e:
print(e)
我懂了:

HTTP Error 410: Gone Traceback (most recent call last): File "e.py",line 9, in yt_obj = YouTube(url) File "/home/jdoe/path_dir/lib/python3.8/site-packages/pytube/main.py",line 91, in initself.prefetch() File "/home/jdoe/path_dir/lib/python3.8/site-packages/pytube/main.py",line 181, in prefetchself.vid_info_raw = request.get(self.vid_info_url) File "/home/jdoe/path_dir/lib/python3.8/site-packages/pytube/request.py",line 36, in getreturn _execute_request(url).read().decode("utf-8") File "/home/jdoe/path_dir/lib/python3.8/site-packages/pytube/request.py",line 24, in _execute_requestreturn urlopen(request) # nosec File "/usr/lib/python3.8/urllib/request.py", line 222, in urlopenreturn opener.open(url, data, timeout) File "/usr/lib/python3.8/urllib/request.py", line 531, in openresponse = meth(req, response) File "/usr/lib/python3.8/urllib/request.py", line 640, in http_responseresponse = self.parent.error( File "/usr/lib/python3.8/urllib/request.py", line 563, in errorresult = self._call_chain(*args) File "/usr/lib/python3.8/urllib/request.py", line 502, in _call_chainresult = func(*args) File "/usr/lib/python3.8/urllib/request.py", line 755, in http_error_302return self.parent.open(new, timeout=req.timeout) File "/usr/lib/python3.8/urllib/request.py", line 531, in openresponse = meth(req, response) File "/usr/lib/python3.8/urllib/request.py", line 640, in http_responseresponse = self.parent.error( File "/usr/lib/python3.8/urllib/request.py", line 569, in errorreturn self._call_chain(*args) File "/usr/lib/python3.8/urllib/request.py", line 502, in _call_chainresult = func(*args) File "/usr/lib/python3.8/urllib/request.py", line 649, inhttp_error_defaultraise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 410:Gone

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "e.py", line 14, in raise Exception('Some exception occurred.') Exception: Some exception occurred.


我的研究表明 410 错误涉及一个 URL 被找到但随后被移动。由于问题发生在不同的 youtube 链接和不同的程序上,我不知所措。当我不断收到错误 410 消息时,如何下载 YouTube 视频?

最佳答案

有一个bug in pytube3这会导致 410 错误。有一个从该问题链接的修复程序似乎可以解决该问题。如果我安装补丁版本:

pip install git+https://github.com/Zeecka/pytube@fix_1060#egg=pytube
然后我可以获取有关 youtube 视频的信息而不会出现该错误:
>>> import pytube
>>> yt = pytube.YouTube('https://www.youtube.com/watch?v=DkU9WFj8sYo')
>>> yt.title
'Space Ranger Video Game using FXGL and JavaFX'
修复已作为 pull request 提交.通读讨论,似乎仍有一些 Unresolved 问题。

关于python - 如何使用 Python 下载 YouTube 视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68606981/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com