{"id":1188267,"date":"2025-01-15T20:15:12","date_gmt":"2025-01-15T12:15:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1188267.html"},"modified":"2025-01-15T20:15:14","modified_gmt":"2025-01-15T12:15:14","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e4%b8%8b%e8%bd%bd%e6%b2%90%e6%b5%b4%e4%b9%8b%e7%8e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1188267.html","title":{"rendered":"\u5982\u4f55\u7528Python\u4e0b\u8f7d\u6c90\u6d74\u4e4b\u738b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140629\/ab7e5b71-4cc3-4a3b-89dc-4d7062ae4e60.webp\" alt=\"\u5982\u4f55\u7528Python\u4e0b\u8f7d\u6c90\u6d74\u4e4b\u738b\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u4e0b\u8f7d\u300a\u6c90\u6d74\u4e4b\u738b\u300b\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982requests\u6216BeautifulSoup\u8fdb\u884c\u7f51\u9875\u6293\u53d6\u3001\u4f7f\u7528\u89c6\u9891\u4e0b\u8f7d\u5de5\u5177\u5982youtube-dl\u4e0effmpeg\u3001\u5229\u7528Selenium\u8fdb\u884c\u81ea\u52a8\u5316\u64cd\u4f5c\u7b49<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5229\u7528youtube-dl\u4e0effmpeg\u8fdb\u884c\u89c6\u9891\u4e0b\u8f7d\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5de5\u5177<\/h3>\n<\/p>\n<p><p><strong>1. \u5b89\u88c5Python<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5728\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86Python\u3002\u53ef\u4ee5\u901a\u8fc7\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684Python\uff0c\u5e76\u6309\u7167\u63d0\u793a\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u5b89\u88c5youtube-dl<\/strong><\/p>\n<\/p>\n<p><p>youtube-dl\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u4e0b\u8f7dYouTube\u89c6\u9891\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7pip\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install youtube-dl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u5b89\u88c5ffmpeg<\/strong><\/p>\n<\/p>\n<p><p>ffmpeg\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u591a\u5a92\u4f53\u6846\u67b6\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u89c6\u9891\u3001\u97f3\u9891\u548c\u5176\u4ed6\u591a\u5a92\u4f53\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u4ece\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u5b83\uff0c\u5e76\u786e\u4fdd\u5c06\u5176\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528youtube-dl\u4e0b\u8f7d\u89c6\u9891<\/h3>\n<\/p>\n<p><p><strong>1. \u83b7\u53d6\u89c6\u9891\u94fe\u63a5<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u83b7\u53d6\u300a\u6c90\u6d74\u4e4b\u738b\u300b\u89c6\u9891\u7684\u94fe\u63a5\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u89c6\u9891\u7f51\u7ad9\u6765\u627e\u5230\u8be5\u89c6\u9891\u7684URL\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528youtube-dl\u4e0b\u8f7d\u89c6\u9891<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4e0b\u8f7d\u89c6\u9891\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">youtube-dl &lt;video_url&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06<code>&lt;video_url&gt;<\/code>\u66ff\u6362\u4e3a\u4f60\u83b7\u53d6\u7684\u89c6\u9891\u94fe\u63a5\u3002youtube-dl\u5c06\u81ea\u52a8\u89e3\u6790\u89c6\u9891\u94fe\u63a5\u5e76\u4e0b\u8f7d\u89c6\u9891\u6587\u4ef6\u5230\u5f53\u524d\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528ffmpeg\u5904\u7406\u89c6\u9891<\/h3>\n<\/p>\n<p><p><strong>1. \u8f6c\u6362\u89c6\u9891\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5c06\u4e0b\u8f7d\u7684\u89c6\u9891\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528ffmpeg\u3002\u4f8b\u5982\uff0c\u5c06\u89c6\u9891\u8f6c\u6362\u4e3aMP4\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ffmpeg -i input_video.mkv output_video.mp4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06<code>input_video.mkv<\/code>\u66ff\u6362\u4e3a\u4f60\u4e0b\u8f7d\u7684\u89c6\u9891\u6587\u4ef6\u540d\uff0c<code>output_video.mp4<\/code>\u4e3a\u4f60\u60f3\u8981\u8f93\u51fa\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u81ea\u52a8\u5316\u4e0b\u8f7d\u6d41\u7a0b<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u81ea\u52a8\u5316\u6574\u4e2a\u4e0b\u8f7d\u6d41\u7a0b\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2aPython\u811a\u672c\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import youtube_dl<\/p>\n<h2><strong>\u89c6\u9891\u94fe\u63a5<\/strong><\/h2>\n<p>video_url = &quot;https:\/\/www.example.com\/video&quot;<\/p>\n<h2><strong>\u4e0b\u8f7d\u89c6\u9891\u7684\u9009\u9879<\/strong><\/h2>\n<p>ydl_opts = {<\/p>\n<p>    &#39;format&#39;: &#39;best&#39;,<\/p>\n<p>    &#39;outtmpl&#39;: &#39;downloaded_video.%(ext)s&#39;,<\/p>\n<p>}<\/p>\n<h2><strong>\u4f7f\u7528youtube-dl\u4e0b\u8f7d\u89c6\u9891<\/strong><\/h2>\n<p>with youtube_dl.YoutubeDL(ydl_opts) as ydl:<\/p>\n<p>    ydl.download([video_url])<\/p>\n<h2><strong>\u4f7f\u7528ffmpeg\u8f6c\u6362\u89c6\u9891\u683c\u5f0f<\/strong><\/h2>\n<p>os.system(&quot;ffmpeg -i downloaded_video.mkv output_video.mp4&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p><strong>1. \u5408\u6cd5\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u8f7d\u89c6\u9891\u65f6\uff0c\u8bf7\u786e\u4fdd\u4f60\u6709\u5408\u6cd5\u7684\u6743\u9650\u3002\u672a\u7ecf\u6388\u6743\u4e0b\u8f7d\u548c\u5206\u53d1\u89c6\u9891\u53ef\u80fd\u4f1a\u8fdd\u53cd\u7248\u6743\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4f9d\u8d56\u7ba1\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u5b89\u88c5\u7684\u7b2c\u4e09\u65b9\u5e93\u548c\u5de5\u5177\u90fd\u662f\u6700\u65b0\u7248\u672c\uff0c\u4ee5\u907f\u514d\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u9519\u8bef\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u3002\u4f60\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u4ee3\u7801\uff0c\u4ee5\u63d0\u9ad8\u5065\u58ee\u6027\u3002\u4f8b\u5982\uff0c\u6355\u83b7\u4e0b\u8f7d\u5931\u8d25\u7684\u5f02\u5e38\u5e76\u91cd\u65b0\u5c1d\u8bd5\u4e0b\u8f7d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u4e0b\u8f7d\u300a\u6c90\u6d74\u4e4b\u738b\u300b\u89c6\u9891\uff0c\u5e76\u4f7f\u7528ffmpeg\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u300a\u6c90\u6d74\u4e4b\u738b\u300b\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u4e0b\u8f7d\u548c\u5904\u7406\u5176\u4ed6\u5728\u7ebf\u89c6\u9891\u3002\u5e0c\u671b\u8fd9\u4e2a\u6307\u5357\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6df1\u5165\u7406\u89e3\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><p><strong>1. youtube-dl\u914d\u7f6e<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u4e0b\u8f7d\u529f\u80fd\uff0cyoutube-dl\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u914d\u7f6e\u9009\u9879\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u4e0b\u8f7d\u901f\u5ea6\u9650\u5236\u3001\u6307\u5b9a\u4ee3\u7406\u670d\u52a1\u5668\u3001\u4e0b\u8f7d\u5b57\u5e55\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u914d\u7f6e\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ydl_opts = {<\/p>\n<p>    &#39;format&#39;: &#39;best&#39;,<\/p>\n<p>    &#39;outtmpl&#39;: &#39;downloaded_video.%(ext)s&#39;,<\/p>\n<p>    &#39;proxy&#39;: &#39;http:\/\/127.0.0.1:8080&#39;,  # \u4f7f\u7528\u4ee3\u7406\u670d\u52a1\u5668<\/p>\n<p>    &#39;subtitleslangs&#39;: [&#39;en&#39;, &#39;zh&#39;],  # \u4e0b\u8f7d\u5b57\u5e55<\/p>\n<p>    &#39;subtitlesformat&#39;: &#39;srt&#39;,  # \u5b57\u5e55\u683c\u5f0f<\/p>\n<p>    &#39;ratelimit&#39;: 1024 * 1024,  # \u4e0b\u8f7d\u901f\u5ea6\u9650\u5236\uff081MB\/s\uff09<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528Selenium\u8fdb\u884c\u81ea\u52a8\u5316\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u89c6\u9891\u53ef\u80fd\u9700\u8981\u767b\u5f55\u6216\u8fdb\u884c\u4e00\u4e9b\u4ea4\u4e92\u64cd\u4f5c\u624d\u80fd\u4e0b\u8f7d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5Selenium\u548c\u6d4f\u89c8\u5668\u9a71\u52a8<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u8f7d\u5e76\u5b89\u88c5\u5bf9\u5e94\u6d4f\u89c8\u5668\u7684\u9a71\u52a8\u7a0b\u5e8f\uff08\u5982ChromeDriver\uff09\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Selenium\u767b\u5f55\u5e76\u4e0b\u8f7d\u89c6\u9891\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.common.keys import Keys<\/p>\n<p>import time<\/p>\n<h2><strong>\u914d\u7f6e\u6d4f\u89c8\u5668\u9a71\u52a8<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;\/path\/to\/chromedriver&#39;)<\/p>\n<h2><strong>\u6253\u5f00\u89c6\u9891\u7f51\u7ad9<\/strong><\/h2>\n<p>driver.get(&#39;https:\/\/www.example.com&#39;)<\/p>\n<h2><strong>\u767b\u5f55<\/strong><\/h2>\n<p>username = driver.find_element(By.ID, &#39;username&#39;)<\/p>\n<p>password = driver.find_element(By.ID, &#39;password&#39;)<\/p>\n<p>login_button = driver.find_element(By.ID, &#39;loginButton&#39;)<\/p>\n<p>username.send_keys(&#39;your_username&#39;)<\/p>\n<p>password.send_keys(&#39;your_password&#39;)<\/p>\n<p>login_button.click()<\/p>\n<h2><strong>\u7b49\u5f85\u767b\u5f55\u5b8c\u6210<\/strong><\/h2>\n<p>time.sleep(5)<\/p>\n<h2><strong>\u627e\u5230\u89c6\u9891\u5e76\u83b7\u53d6\u94fe\u63a5<\/strong><\/h2>\n<p>video_element = driver.find_element(By.XPATH, &#39;\/\/a[@href=&quot;\/video\/123&quot;]&#39;)<\/p>\n<p>video_url = video_element.get_attribute(&#39;href&#39;)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<h2><strong>\u4f7f\u7528youtube-dl\u4e0b\u8f7d\u89c6\u9891<\/strong><\/h2>\n<p>import youtube_dl<\/p>\n<p>ydl_opts = {<\/p>\n<p>    &#39;format&#39;: &#39;best&#39;,<\/p>\n<p>    &#39;outtmpl&#39;: &#39;downloaded_video.%(ext)s&#39;,<\/p>\n<p>}<\/p>\n<p>with youtube_dl.YoutubeDL(ydl_opts) as ydl:<\/p>\n<p>    ydl.download([video_url])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u4f7f\u7528ffmpeg\u8fdb\u884c\u9ad8\u7ea7\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>ffmpeg\u4e0d\u4ec5\u53ef\u4ee5\u8f6c\u6362\u89c6\u9891\u683c\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u526a\u8f91\u3001\u5408\u5e76\u3001\u6dfb\u52a0\u6c34\u5370\u7b49\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u526a\u8f91\u89c6\u9891\u7684\u524d30\u79d2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ffmpeg -i input_video.mp4 -t 30 -c copy output_video.mp4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\uff0c\u5408\u5e76\u591a\u4e2a\u89c6\u9891\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ffmpeg -f concat -<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a> 0 -i filelist.txt -c copy output_video.mp4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>filelist.txt<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u5f85\u5408\u5e76\u89c6\u9891\u6587\u4ef6\u5217\u8868\u7684\u6587\u672c\u6587\u4ef6\uff0c\u6bcf\u884c\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5b9e\u8df5\u4e0e\u4f18\u5316<\/h3>\n<\/p>\n<p><p><strong>1. \u89c6\u9891\u4e0b\u8f7d\u4e0e\u5904\u7406\u7684\u6279\u91cf\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6279\u91cf\u4e0b\u8f7d\u548c\u5904\u7406\u591a\u4e2a\u89c6\u9891\uff0c\u53ef\u4ee5\u7f16\u5199\u811a\u672c\u5faa\u73af\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">video_urls = [<\/p>\n<p>    &#39;https:\/\/www.example.com\/video1&#39;,<\/p>\n<p>    &#39;https:\/\/www.example.com\/video2&#39;,<\/p>\n<p>    &#39;https:\/\/www.example.com\/video3&#39;,<\/p>\n<p>]<\/p>\n<p>for url in video_urls:<\/p>\n<p>    with youtube_dl.YoutubeDL(ydl_opts) as ydl:<\/p>\n<p>        ydl.download([url])<\/p>\n<p>    os.system(&quot;ffmpeg -i downloaded_video.mkv output_video.mp4&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u591a\u7ebf\u7a0b\u4e0e\u5e76\u884c\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4e0b\u8f7d\u548c\u5904\u7406\u6548\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5e76\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>concurrent.futures<\/code>\u8fdb\u884c\u591a\u7ebf\u7a0b\u4e0b\u8f7d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>def download_video(url):<\/p>\n<p>    with youtube_dl.YoutubeDL(ydl_opts) as ydl:<\/p>\n<p>        ydl.download([url])<\/p>\n<p>    os.system(&quot;ffmpeg -i downloaded_video.mkv output_video.mp4&quot;)<\/p>\n<p>with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:<\/p>\n<p>    executor.map(download_video, video_urls)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u6027\u80fd\u4f18\u5316\u4e0e<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5927\u89c4\u6a21\u89c6\u9891\u4e0b\u8f7d\u548c\u5904\u7406\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6027\u80fd\u4f18\u5316\u4e0e\u8d44\u6e90\u7ba1\u7406\u3002\u4f8b\u5982\uff0c\u63a7\u5236\u5e76\u53d1\u6570\u91cf\u3001\u76d1\u63a7\u7cfb\u7edf\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\u3001\u5904\u7406\u78c1\u76d8I\/O\u74f6\u9888\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u4e0e\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u4f7f\u7528Python\u4e0b\u8f7d\u548c\u5904\u7406\u300a\u6c90\u6d74\u4e4b\u738b\u300b\u89c6\u9891\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5355\u4e2a\u89c6\u9891\u4e0b\u8f7d\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6279\u91cf\u4e0b\u8f7d\u4e0e\u5904\u7406\uff0c\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u672f\u90fd\u80fd\u5e2e\u52a9\u4f60\u9ad8\u6548\u5b8c\u6210\u4efb\u52a1\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u4e3a\u4f60\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u5e2e\u52a9\u548c\u542f\u793a\u3002\u5982\u679c\u4f60\u6709\u66f4\u591a\u9700\u6c42\u6216\u95ee\u9898\uff0c\u6b22\u8fce\u7ee7\u7eed\u63a2\u7d22\u76f8\u5173\u6587\u6863\u548c\u793e\u533a\u8d44\u6e90\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u6587\u4ef6\u4e0b\u8f7d\uff1f<\/strong><br \/>\u4f7f\u7528Python\u4e0b\u8f7d\u6587\u4ef6\u901a\u5e38\u6d89\u53ca\u5230\u4f7f\u7528requests\u5e93\u6216urllib\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u5b89\u88c5requests\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install requests<\/code>\u6765\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u811a\u672c\uff0c\u4f7f\u7528requests.get()\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528\u6253\u5f00\u6587\u4ef6\u7684\u5199\u5165\u6a21\u5f0f\u5c06\u5176\u4fdd\u5b58\u5230\u672c\u5730\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\n\nurl = &#39;\u4e0b\u8f7d\u94fe\u63a5&#39;\nresponse = requests.get(url)\n\nwith open(&#39;\u6587\u4ef6\u540d&#39;, &#39;wb&#39;) as file:\n    file.write(response.content)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u4e0b\u8f7d\u7684\u6587\u4ef6\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u4e0b\u8f7d\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u4e86\u89e3\u6587\u4ef6\u7684\u683c\u5f0f\uff0c\u4ee5\u4fbf\u6b63\u786e\u5904\u7406\u3002\u5e38\u89c1\u7684\u683c\u5f0f\u5305\u62ec\u6587\u672c\u6587\u4ef6\u3001\u56fe\u7247\u3001\u97f3\u9891\u7b49\u3002\u9488\u5bf9\u4e0d\u540c\u7c7b\u578b\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u5e93\u6765\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4e0b\u8f7d\u56fe\u7247\u540e\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u8fdb\u884c\u56fe\u50cf\u5904\u7406\uff0c\u4e0b\u8f7d\u97f3\u9891\u6587\u4ef6\u65f6\u53ef\u4ee5\u4f7f\u7528pydub\u7b49\u5e93\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4e0b\u8f7d\u5927\u578b\u6587\u4ef6\u65f6\u5982\u4f55\u4f18\u5316Python\u4ee3\u7801\u6027\u80fd\uff1f<\/strong><br \/>\u4e0b\u8f7d\u5927\u578b\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5206\u5757\u4e0b\u8f7d\u6765\u4f18\u5316\u6027\u80fd\u3002\u4f7f\u7528<code>stream=True<\/code>\u53c2\u6570\u8fdb\u884c\u8bf7\u6c42\uff0c\u5229\u7528<code>iter_content()<\/code>\u65b9\u6cd5\u5206\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u63d0\u9ad8\u4e0b\u8f7d\u6548\u7387\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\n\nurl = &#39;\u4e0b\u8f7d\u94fe\u63a5&#39;\nresponse = requests.get(url, stream=True)\n\nwith open(&#39;\u6587\u4ef6\u540d&#39;, &#39;wb&#39;) as file:\n    for chunk in response.iter_content(chunk_size=8192):\n        file.write(chunk)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u9ad8\u6548\u5730\u4e0b\u8f7d\u6587\u4ef6\u5e76\u5904\u7406\u4e0d\u540c\u683c\u5f0f\uff0c\u786e\u4fdd\u4f60\u7684\u4ee3\u7801\u65e2\u6027\u80fd\u4f18\u8d8a\u53c8\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u4e0b\u8f7d\u300a\u6c90\u6d74\u4e4b\u738b\u300b\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982requests\u6216BeautifulSoup\u8fdb\u884c\u7f51\u9875 [&hellip;]","protected":false},"author":3,"featured_media":1188271,"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\/1188267"}],"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=1188267"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188267\/revisions"}],"predecessor-version":[{"id":1188273,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188267\/revisions\/1188273"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1188271"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1188267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1188267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1188267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}