Windows 10下如何为pytube添加代码绕过YouTube年龄限制?
解决Pytube下载年龄限制YouTube视频的问题
问题场景
在Windows 10系统中使用pytube下载YouTube视频(链接:https://www.youtube.com/watch?v=cw2vlMK9P7U)时,触发以下错误:
Traceback (most recent call last): File "c:\Users\1005447\ytdown.py", line 15, in <module> streams = vid.streams.filter(file_extension='mp4') ^^^^^^^^^^^ File "C:\Users\1005447\AppData\Local\Programs\Python\Python312\Lib\site-packages\pytube\__main__.py", line 296, in streams return StreamQuery(self.fmt_streams) ^^^^^^^^^^^^^^^^ File "C:\Users\1005447\AppData\Local\Programs\Python\Python312\Lib\site-packages\pytube\__main__.py", line 176, in fmt_streams stream_manifest = extract.apply_descrambler(self.streaming_data) ^^^^^^^^^^^^^^^^^^^ File "C:\Users\1005447\AppData\Local\Programs\Python\Python312\Lib\site-packages\pytube\__main__.py", line 160, in streaming_data self.bypass_age_gate() File "C:\Users\1005447\AppData\Local\Programs\Python\Python312\Lib\site-packages\pytube\__main__.py", line 264, in bypass_age_gate raise exceptions.AgeRestrictedError(self.video_id) pytube.exceptions.AgeRestrictedError: cw2vlMK9P7U is age restricted, and can't be accessed without logging in.
用户使用的源代码:
import os import pytube as pt import time as t urltu = input("Enter the full link to the video you wish to download --> ") qualin = input("""Which quality do you wish to download? Type H if you want highest Type L if you want lowest Type your answer here --> """) qualin = qualin.upper() Monostate = {"allow_ratelimiting": True} vid = pt.YouTube(urltu) videoid = vid.video_id streams = vid.streams.filter(file_extension='mp4') if qualin == "H": stream = streams.get_by_itag(streams.first().itag) filenamed = f"{vid.title}_highqual.mp4" elif qualin == "L": stream = streams.get_by_itag(streams.last()) filenamed = f"{vid.title}_lowqual.mp4" else: print("Invalid choice. Exiting.") exit() stream.download(filename=filenamed) os.system(f'start "" "{filenamed}"') print(f"Downloaded file. Filename is {filenamed}") t.sleep(30)
用户无法找到innertube.py,询问需向代码添加什么内容绕过年龄限制。
解决方案
无需修改库文件,通过给pytube的YouTube对象传入已登录YouTube的cookie即可绕过限制,具体操作如下:
1. 获取YouTube登录cookie
- 打开浏览器登录YouTube,访问目标视频页面
- 按F12打开开发者工具,切换到「应用程序」标签
- 在左侧「存储」>「Cookies」>「https://www.youtube.com」中,找到
__Secure-3PSID和__Secure-3PAPISID两个cookie的值并复制
2. 修改代码
在创建pt.YouTube对象时,添加cookie和模拟浏览器的请求头参数,完整修改后的代码如下:
import os import pytube as pt import time as t urltu = input("Enter the full link to the video you wish to download --> ") qualin = input("""Which quality do you wish to download? Type H if you want highest Type L if you want lowest Type your answer here --> """) qualin = qualin.upper() # 替换为你自己的cookie值 cookies = { "__Secure-3PSID": "你的__Secure-3PSID值", "__Secure-3PAPISID": "你的__Secure-3PAPISID值" } # 模拟浏览器请求头 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" } Monostate = {"allow_ratelimiting": True} # 传入cookie和请求头初始化YouTube对象 vid = pt.YouTube(urltu, cookies=cookies, headers=headers) videoid = vid.video_id streams = vid.streams.filter(file_extension='mp4') if qualin == "H": stream = streams.get_by_itag(streams.first().itag) filenamed = f"{vid.title}_highqual.mp4" elif qualin == "L": stream = streams.get_by_itag(streams.last().itag) filenamed = f"{vid.title}_lowqual.mp4" else: print("Invalid choice. Exiting.") exit() stream.download(filename=filenamed) os.system(f'start "" "{filenamed}"') print(f"Downloaded file. Filename is {filenamed}") t.sleep(30)
注意事项
__Secure-3PSID和__Secure-3PAPISID会定期失效,若出现访问失败,重新获取最新值即可- 确保你的YouTube账号已完成年龄验证,能够正常访问目标视频
内容的提问来源于stack exchange,提问作者Bella L.
相关产品推荐
相关产品推荐

