{"id":1049898,"date":"2024-12-31T14:03:10","date_gmt":"2024-12-31T06:03:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1049898.html"},"modified":"2024-12-31T14:03:13","modified_gmt":"2024-12-31T06:03:13","slug":"python%e5%a6%82%e4%bd%95%e4%bb%8e%e5%ae%98%e7%bd%91%e4%b8%8a%e4%b8%8b%e8%bd%bd%e8%a7%86%e9%a2%91","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1049898.html","title":{"rendered":"python\u5982\u4f55\u4ece\u5b98\u7f51\u4e0a\u4e0b\u8f7d\u89c6\u9891"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5888a904-6928-4ec8-be88-2285187240a0.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4ece\u5b98\u7f51\u4e0a\u4e0b\u8f7d\u89c6\u9891\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u4ece\u5b98\u7f51\u4e0a\u4e0b\u8f7d\u89c6\u9891\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>requests<\/code>\u5e93\u3001<code>youtube-dl<\/code>\u5e93\u3001<code>pytube<\/code>\u5e93\u3001\u4ee5\u53ca<code>selenium<\/code>\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>pytube<\/code>\u5e93\u662f\u6700\u4e3a\u65b9\u4fbf\u548c\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e13\u95e8\u7528\u4e8e\u4eceYouTube\u4e0a\u4e0b\u8f7d\u89c6\u9891\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>pytube<\/code>\u5e93\u6765\u4e0b\u8f7d\u89c6\u9891\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5<code>pytube<\/code>\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5<code>pytube<\/code>\u5e93\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pytube<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>pytube<\/code>\u4e0b\u8f7d\u89c6\u9891<\/h3>\n<\/p>\n<p><p><code>pytube<\/code>\u5e93\u53ef\u4ee5\u8ba9\u4f60\u8f7b\u677e\u5730\u4eceYouTube\u4e0a\u4e0b\u8f7d\u89c6\u9891\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pytube import YouTube<\/p>\n<h2><strong>\u8f93\u5165\u8981\u4e0b\u8f7d\u7684\u89c6\u9891\u7684URL<\/strong><\/h2>\n<p>url = &#39;https:\/\/www.youtube.com\/watch?v=9bZkp7q19f0&#39;<\/p>\n<h2><strong>\u521b\u5efaYouTube\u5bf9\u8c61<\/strong><\/h2>\n<p>yt = YouTube(url)<\/p>\n<h2><strong>\u83b7\u53d6\u6700\u9ad8\u8d28\u91cf\u7684\u89c6\u9891\u6d41<\/strong><\/h2>\n<p>stream = yt.streams.get_highest_resolution()<\/p>\n<h2><strong>\u4e0b\u8f7d\u89c6\u9891\u5230\u5f53\u524d\u76ee\u5f55<\/strong><\/h2>\n<p>stream.download()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u811a\u672c\u5c06\u4ece\u7ed9\u5b9a\u7684YouTube URL\u4e2d\u4e0b\u8f7d\u6700\u9ad8\u8d28\u91cf\u7684\u89c6\u9891\uff0c\u5e76\u4fdd\u5b58\u5230\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u9009\u62e9\u7279\u5b9a\u683c\u5f0f\u548c\u5206\u8fa8\u7387<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u9009\u62e9\u7279\u5b9a\u7684\u683c\u5f0f\u548c\u5206\u8fa8\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytube<\/code>\u63d0\u4f9b\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pytube import YouTube<\/p>\n<h2><strong>\u8f93\u5165\u8981\u4e0b\u8f7d\u7684\u89c6\u9891\u7684URL<\/strong><\/h2>\n<p>url = &#39;https:\/\/www.youtube.com\/watch?v=9bZkp7q19f0&#39;<\/p>\n<h2><strong>\u521b\u5efaYouTube\u5bf9\u8c61<\/strong><\/h2>\n<p>yt = YouTube(url)<\/p>\n<h2><strong>\u5217\u51fa\u6240\u6709\u53ef\u7528\u7684\u6d41<\/strong><\/h2>\n<p>streams = yt.streams.filter(progressive=True, file_extension=&#39;mp4&#39;)<\/p>\n<p>for stream in streams:<\/p>\n<p>    print(stream)<\/p>\n<h2><strong>\u9009\u62e9\u7279\u5b9a\u7684\u6d41<\/strong><\/h2>\n<p>stream = yt.streams.get_by_itag(22)  # \u4f8b\u5982itag 22\u901a\u5e38\u662f720p<\/p>\n<h2><strong>\u4e0b\u8f7d\u89c6\u9891\u5230\u5f53\u524d\u76ee\u5f55<\/strong><\/h2>\n<p>stream.download()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4e0b\u8f7d\u97f3\u9891<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u53ea\u9700\u8981\u4e0b\u8f7d\u89c6\u9891\u7684\u97f3\u9891\u90e8\u5206\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pytube import YouTube<\/p>\n<h2><strong>\u8f93\u5165\u8981\u4e0b\u8f7d\u7684\u89c6\u9891\u7684URL<\/strong><\/h2>\n<p>url = &#39;https:\/\/www.youtube.com\/watch?v=9bZkp7q19f0&#39;<\/p>\n<h2><strong>\u521b\u5efaYouTube\u5bf9\u8c61<\/strong><\/h2>\n<p>yt = YouTube(url)<\/p>\n<h2><strong>\u83b7\u53d6\u97f3\u9891\u6d41<\/strong><\/h2>\n<p>audio_stream = yt.streams.filter(only_audio=True).first()<\/p>\n<h2><strong>\u4e0b\u8f7d\u97f3\u9891\u5230\u5f53\u524d\u76ee\u5f55<\/strong><\/h2>\n<p>audio_stream.download(filename=&#39;audio.mp3&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u4e0b\u8f7d\u8fdb\u5ea6<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6dfb\u52a0\u8fdb\u5ea6\u6761\u6765\u663e\u793a\u4e0b\u8f7d\u8fdb\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pytube import YouTube<\/p>\n<p>from pytube.cli import on_progress<\/p>\n<h2><strong>\u8f93\u5165\u8981\u4e0b\u8f7d\u7684\u89c6\u9891\u7684URL<\/strong><\/h2>\n<p>url = &#39;https:\/\/www.youtube.com\/watch?v=9bZkp7q19f0&#39;<\/p>\n<h2><strong>\u521b\u5efaYouTube\u5bf9\u8c61<\/strong><\/h2>\n<p>yt = YouTube(url, on_progress_callback=on_progress)<\/p>\n<h2><strong>\u83b7\u53d6\u6700\u9ad8\u8d28\u91cf\u7684\u89c6\u9891\u6d41<\/strong><\/h2>\n<p>stream = yt.streams.get_highest_resolution()<\/p>\n<h2><strong>\u4e0b\u8f7d\u89c6\u9891\u5230\u5f53\u524d\u76ee\u5f55<\/strong><\/h2>\n<p>stream.download()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u8f7b\u677e\u5730\u4eceYouTube\u5b98\u7f51\u4e0a\u4e0b\u8f7d\u89c6\u9891\u3002<code>pytube<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u4e0b\u8f7d\u9700\u6c42\uff0c\u5982\u9009\u62e9\u7279\u5b9a\u683c\u5f0f\u3001\u5206\u8fa8\u7387\u4ee5\u53ca\u4ec5\u4e0b\u8f7d\u97f3\u9891\u7b49\u3002\u5982\u679c\u4f60\u9700\u8981\u4ece\u5176\u4ed6\u7f51\u7ad9\u4e0b\u8f7d\u89c6\u9891\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>requests<\/code>\u5e93\u548c<code>BeautifulSoup<\/code>\u5e93\u8fdb\u884c\u7f51\u9875\u89e3\u6790\u548c\u6570\u636e\u6293\u53d6\uff0c\u4f46\u8fd9\u9700\u8981\u66f4\u591a\u7684\u7f16\u7a0b\u6280\u5de7\u548c\u7406\u89e3\u76ee\u6807\u7f51\u7ad9\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5e93\u6765\u4e0b\u8f7d\u89c6\u9891\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u4e0b\u8f7d\u89c6\u9891\uff0c\u5982<code>requests<\/code>\u548c<code>youtube_dl<\/code>\u3002\u4f7f\u7528<code>requests<\/code>\u5e93\u65f6\uff0c\u4f60\u53ef\u4ee5\u53d1\u9001HTTP\u8bf7\u6c42\u6765\u83b7\u53d6\u89c6\u9891\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5199\u5165\u672c\u5730\u6587\u4ef6\u3002<code>youtube_dl<\/code>\u5e93\u5219\u4e13\u95e8\u7528\u4e8e\u4eceYouTube\u53ca\u5176\u4ed6\u89c6\u9891\u5e73\u53f0\u4e0b\u8f7d\u89c6\u9891\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u6307\u5b9a\u89c6\u9891\u7684URL\u5373\u53ef\u3002<\/p>\n<p><strong>\u4e0b\u8f7d\u89c6\u9891\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4e0b\u8f7d\u89c6\u9891\u65f6\uff0c\u786e\u4fdd\u4f60\u9075\u5faa\u7248\u6743\u6cd5\u89c4\u3002\u67d0\u4e9b\u89c6\u9891\u5e73\u53f0\u7981\u6b62\u4e0b\u8f7d\u5176\u5185\u5bb9\uff0c\u6216\u8005\u8981\u6c42\u83b7\u5f97\u6388\u6743\u3002\u6b64\u5916\uff0c\u7f51\u7edc\u8fde\u63a5\u7684\u7a33\u5b9a\u6027\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u82e5\u8fde\u63a5\u4e0d\u7a33\u5b9a\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0b\u8f7d\u5931\u8d25\u6216\u89c6\u9891\u6587\u4ef6\u635f\u574f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e0b\u8f7d\u540e\u7684\u89c6\u9891\u683c\u5f0f\u95ee\u9898\uff1f<\/strong><br \/>\u4e0b\u8f7d\u7684\u89c6\u9891\u53ef\u80fd\u4f1a\u4ee5\u4e0d\u540c\u7684\u683c\u5f0f\u4fdd\u5b58\uff0c\u4f8b\u5982MP4\u3001AVI\u6216MKV\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>ffmpeg<\/code>\u7b49\u5de5\u5177\u6765\u8f6c\u6362\u89c6\u9891\u683c\u5f0f\uff0c\u786e\u4fdd\u5176\u5728\u6240\u9700\u8bbe\u5907\u4e0a\u64ad\u653e\u65f6\u6ca1\u6709\u517c\u5bb9\u6027\u95ee\u9898\u3002\u8f6c\u6362\u65f6\uff0c\u6ce8\u610f\u9009\u62e9\u9002\u5408\u7684\u7f16\u7801\u8bbe\u7f6e\uff0c\u4ee5\u4fdd\u6301\u89c6\u9891\u8d28\u91cf\u548c\u6587\u4ef6\u5927\u5c0f\u7684\u5e73\u8861\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u4ece\u5b98\u7f51\u4e0a\u4e0b\u8f7d\u89c6\u9891\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528requests\u5e93\u3001youtube-dl\u5e93\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1049908,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049898"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1049898"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049898\/revisions"}],"predecessor-version":[{"id":1049910,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049898\/revisions\/1049910"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1049908"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1049898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1049898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1049898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}