{"id":176137,"date":"2024-05-08T19:04:37","date_gmt":"2024-05-08T11:04:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176137.html"},"modified":"2024-05-08T19:04:44","modified_gmt":"2024-05-08T11:04:44","slug":"%e4%bd%bf%e7%94%a8python%e6%80%8e%e4%b9%88%e4%b8%8b%e8%bd%bd%e8%a7%86%e9%a2%91","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/176137.html","title":{"rendered":"\u4f7f\u7528python\u600e\u4e48\u4e0b\u8f7d\u89c6\u9891"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27055646\/bba4179a-65d1-441d-919d-4a467322fc2b.webp\" alt=\"\u4f7f\u7528python\u600e\u4e48\u4e0b\u8f7d\u89c6\u9891\" \/><\/p>\n<p><p>\u4f7f\u7528Python\u4e0b\u8f7d\u89c6\u9891\u7684\u65b9\u6cd5\u5305\u62ec\u76f4\u63a5\u8bf7\u6c42\u89c6\u9891\u94fe\u63a5\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>pytube<\/code>\u3001\u5229\u7528\u7f51\u9875\u722c\u866b\u6280\u672f\u6293\u53d6\u89c6\u9891\u4fe1\u606f\u7b49\u3002\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>pytube<\/code>\u548c<code>youtube-dl<\/code>\uff0c\u56e0\u4e3a\u5b83\u4eec\u5c01\u88c5\u4e86\u590d\u6742\u7684\u4e0b\u8f7d\u903b\u8f91\uff0c\u7b80\u5316\u4e86\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u8ba8\u8bba\u4f7f\u7528<code>pytube<\/code>\u5e93\u4e0b\u8f7dYouTube\u89c6\u9891\u7684\u5b9e\u73b0\u8fc7\u7a0b\uff0c\u56e0\u4e3a\u5b83\u65e2\u7b80\u5355\u53c8\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5PYTUBE\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5<code>pytube<\/code>\u5e93\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u6765\u5b8c\u6210\u3002\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pytube<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u547d\u4ee4\u4f1a\u4ecePython\u5305\u7d22\u5f15\uff08PyPI\uff09\u4e0b\u8f7d<code>pytube<\/code>\u5e76\u5b89\u88c5\u5230\u4f60\u7684\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528PYTUBE\u4e0b\u8f7d\u89c6\u9891<\/h2>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c<code>pytube<\/code>\u5e93\u540e\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199\u811a\u672c\u6765\u4e0b\u8f7d\u89c6\u9891\u4e86\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5bfc\u5165<code>pytube<\/code>\u4e2d\u7684<code>YouTube<\/code>\u7c7b\uff0c\u8fd9\u662f\u8fdb\u884c\u89c6\u9891\u4e0b\u8f7d\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pytube import YouTube<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2aYouTube\u5bf9\u8c61\uff0c\u53c2\u6570\u4e3a\u76ee\u6807\u89c6\u9891\u7684URL\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">yt = YouTube(&#039;\u89c6\u9891\u7684URL&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u9009\u62e9\u89c6\u9891\u7684\u683c\u5f0f\u548c\u6e05\u6670\u5ea6\u3002<code>pytube<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u7b5b\u9009\u683c\u5f0f\uff0c\u4f8b\u5982\u53ef\u4ee5\u9009\u62e9\u4e0b\u8f7d\u5206\u8fa8\u7387\u6700\u9ad8\u7684\u6d41\uff0c\u6216\u8005\u53ea\u4e0b\u8f7d\u97f3\u9891\u6d41\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">stream = yt.streams.get_highest_resolution()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528<code>download()<\/code>\u65b9\u6cd5\u5c06\u89c6\u9891\u4e0b\u8f7d\u5230\u672c\u5730\u3002\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u6307\u5b9a\u4e0b\u8f7d\u76ee\u5f55\uff0c\u9ed8\u8ba4\u76ee\u5f55\u5219\u662f\u5f53\u524d\u8fd0\u884c\u811a\u672c\u7684\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">stream.download()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u4f7f\u7528<code>pytube<\/code>\u4e0b\u8f7dYouTube\u89c6\u9891\u7684\u5b8c\u6574\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pytube import YouTube<\/p>\n<h2><strong>\u8bbe\u7f6e\u89c6\u9891URL<\/strong><\/h2>\n<p>video_url = &#039;https:\/\/www.youtube.com\/watch?v=\u89c6\u9891ID&#039;<\/p>\n<h2><strong>\u521b\u5efaYouTube\u5bf9\u8c61<\/strong><\/h2>\n<p>yt = YouTube(video_url)<\/p>\n<h2><strong>\u9009\u62e9\u6700\u9ad8\u5206\u8fa8\u7387<\/strong><\/h2>\n<p>stream = yt.streams.get_highest_resolution()<\/p>\n<h2><strong>\u4e0b\u8f7d\u89c6\u9891<\/strong><\/h2>\n<p>stream.download()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u5904\u7406\u4e0b\u8f7d\u4e2d\u7684\u5f02\u5e38<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u7f51\u7edc\u95ee\u9898\u3001\u89c6\u9891\u94fe\u63a5\u5931\u6548\u7b49\u3002\u56e0\u6b64\uff0c\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u903b\u8f91\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    yt = YouTube(&#039;\u89c6\u9891\u7684URL&#039;)<\/p>\n<p>    stream = yt.streams.get_highest_resolution()<\/p>\n<p>    stream.download()<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(&#039;\u53d1\u751f\u9519\u8bef\uff1a&#039;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6279\u91cf\u4e0b\u8f7d<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u8981\u4e0b\u8f7d\u591a\u4e2a\u89c6\u9891\uff0c\u53ef\u4ee5\u5c06\u89c6\u9891\u7684URL\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u904d\u5386\u8fd9\u4e2a\u5217\u8868\uff0c\u5bf9\u6bcf\u4e2aURL\u6267\u884c\u4e0b\u8f7d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">urls = [&#039;\u89c6\u9891URL1&#039;, &#039;\u89c6\u9891URL2&#039;, &#039;\u89c6\u9891URL3&#039;]<\/p>\n<p>for url in urls:<\/p>\n<p>    try:<\/p>\n<p>        yt = YouTube(url)<\/p>\n<p>        stream = yt.streams.get_highest_resolution()<\/p>\n<p>        stream.download()<\/p>\n<p>        print(f&#039;{url} \u4e0b\u8f7d\u5b8c\u6210&#039;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&#039;\u5728\u4e0b\u8f7d{url}\u65f6\u53d1\u751f\u9519\u8bef\uff1a&#039;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Python\u548c<code>pytube<\/code>\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u5bb9\u6613\u5730\u4e0b\u8f7dYouTube\u4e0a\u7684\u89c6\u9891\u3002\u9664\u4e86\u4e0a\u8ff0\u7684<code>pytube<\/code>\u5e93\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u5e93\u5982<code>youtube-dl<\/code>\u7b49\u4e5f\u53ef\u4ee5\u5b8c\u6210\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4e0d\u8fc7<code>pytube<\/code>\u4ee5\u5176\u7b80\u5355\u6613\u7528\u800c\u53d7\u5230\u8bb8\u591a\u5f00\u53d1\u8005\u7684\u9752\u7750\u3002\u5728\u8fdb\u884c\u89c6\u9891\u4e0b\u8f7d\u65f6\uff0c\u5207\u8bb0\u9075\u5b88\u76f8\u5173\u5e73\u53f0\u7684\u4f7f\u7528\u6761\u6b3e\u548c\u6761\u4ef6\uff0c\u4e0d\u8981\u4fb5\u72af\u7248\u6743\u6216\u8fdb\u884c\u975e\u6cd5\u4e0b\u8f7d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u89c6\u9891\u4e0b\u8f7d\u5668\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u89c6\u9891\u4e0b\u8f7d\u5668\u53ef\u4ee5\u901a\u8fc7\u5bf9\u7f51\u7edc\u8bf7\u6c42\u8fdb\u884c\u5904\u7406\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u7684requests\u5e93\uff0c\u8be5\u5e93\u4f7f\u5f97\u53d1\u9001HTTP\u8bf7\u6c42\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u7136\u540e\uff0c\u4f7f\u7528requests\u5e93\u53d1\u9001\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u83b7\u53d6\u5230\u89c6\u9891\u7684URL\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528Python\u7684os\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\uff0c\u5c06\u89c6\u9891\u5185\u5bb9\u5199\u5165\u8be5\u6587\u4ef6\u4e2d\u3002\u6700\u540e\uff0c\u4f7f\u7528Python\u7684urllib\u6a21\u5757\u4e0b\u8f7d\u89c6\u9891\u6587\u4ef6\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u89c6\u9891\u4e0b\u8f7d\u5668\u3002<\/p>\n<p><strong>2. Python\u80fd\u591f\u4e0b\u8f7d\u54ea\u4e9b\u7c7b\u578b\u7684\u89c6\u9891\u6587\u4ef6\uff1f<\/strong><\/p>\n<p>Python\u53ef\u4ee5\u4e0b\u8f7d\u51e0\u4e4e\u6240\u6709\u7c7b\u578b\u7684\u89c6\u9891\u6587\u4ef6\u3002\u65e0\u8bba\u662fMP4\u3001AVI\u3001WMV\u8fd8\u662fMKV\uff0cPython\u90fd\u53ef\u4ee5\u901a\u8fc7\u6b63\u786e\u7684\u5904\u7406\u6765\u4e0b\u8f7d\u8fd9\u4e9b\u89c6\u9891\u6587\u4ef6\u3002\u4f7f\u7528Python\u7684requests\u5e93\u548curllib\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u4e0b\u8f7d\u6765\u81ea\u4efb\u4f55\u7f51\u7ad9\u7684\u89c6\u9891\uff0c\u53ea\u8981\u4f60\u6709\u8be5\u89c6\u9891\u7684URL\u3002<\/p>\n<p><strong>3. \u5728\u4f7f\u7528Python\u8fdb\u884c\u89c6\u9891\u4e0b\u8f7d\u65f6\uff0c\u5982\u4f55\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528Python\u8fdb\u884c\u89c6\u9891\u4e0b\u8f7d\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u4e0b\u8f7d\u94fe\u63a5\u5931\u6548\u3001\u7f51\u7edc\u8fde\u63a5\u4e2d\u65ad\u6216\u670d\u52a1\u5668\u9519\u8bef\u7b49\u7b49\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684try-except\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u63aa\u65bd\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5728\u94fe\u63a5\u5931\u6548\u65f6\u5411\u7528\u6237\u663e\u793a\u4e00\u6761\u9519\u8bef\u6d88\u606f\uff0c\u5e76\u63d0\u4f9b\u66f4\u591a\u7684\u5e2e\u52a9\u3002\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u65e5\u5fd7\u8bb0\u5f55\u6a21\u5757\u6765\u8bb0\u5f55\u4efb\u4f55\u9519\u8bef\u6216\u5f02\u5e38\uff0c\u4ee5\u4fbf\u540e\u7eed\u8fdb\u884c\u5206\u6790\u548c\u6392\u67e5\u3002\u603b\u4e4b\uff0c\u826f\u597d\u7684\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u5904\u7406\u662f\u7f16\u5199\u7a33\u5b9a\u4e14\u53ef\u9760\u7684\u89c6\u9891\u4e0b\u8f7d\u7a0b\u5e8f\u7684\u5173\u952e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u4e0b\u8f7d\u89c6\u9891\u7684\u65b9\u6cd5\u5305\u62ec\u76f4\u63a5\u8bf7\u6c42\u89c6\u9891\u94fe\u63a5\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982pytube\u3001\u5229\u7528\u7f51\u9875\u722c\u866b\u6280\u672f\u6293\u53d6\u89c6\u9891\u4fe1\u606f\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":176146,"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\/176137"}],"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=176137"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176137\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176146"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}