{"id":1036121,"date":"2024-12-31T12:04:24","date_gmt":"2024-12-31T04:04:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1036121.html"},"modified":"2024-12-31T12:04:27","modified_gmt":"2024-12-31T04:04:27","slug":"python-%e5%a6%82%e4%bd%95%e4%b8%8b%e8%bd%bd%e8%a7%86%e9%a2%91%e6%96%87%e4%bb%b6%e5%a4%a7%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1036121.html","title":{"rendered":"python \u5982\u4f55\u4e0b\u8f7d\u89c6\u9891\u6587\u4ef6\u5927\u5c0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5d7af322-4015-479f-9c5f-d8e60ebd7834.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python \u5982\u4f55\u4e0b\u8f7d\u89c6\u9891\u6587\u4ef6\u5927\u5c0f\" \/><\/p>\n<p><p> <strong>Python \u4e0b\u8f7d\u89c6\u9891\u6587\u4ef6\u5927\u5c0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u548c\u65b9\u6cd5\u6765\u4e0b\u8f7d\u89c6\u9891\u6587\u4ef6\u5e76\u83b7\u53d6\u5176\u5927\u5c0f\u3002<strong>\u4f7f\u7528\u5e93\u5982 <code>requests<\/code>\u3001<code>urllib<\/code>\u3001<code>wget<\/code> \u8fdb\u884c\u4e0b\u8f7d\u3001\u901a\u8fc7 <code>os.path.getsize()<\/code> \u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3001\u5904\u7406\u5f02\u5e38\u548c\u8fdb\u5ea6\u6761<\/strong>\u662f\u5e38\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>requests<\/code> \u5e93\u4e0b\u8f7d\u89c6\u9891\u5e76\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/h3>\n<\/p>\n<p><p><code>requests<\/code> \u662f\u4e00\u4e2a\u6d41\u884c\u7684 Python \u5e93\uff0c\u7528\u4e8e\u53d1\u9001 HTTP \u8bf7\u6c42\u3002\u5b83\u975e\u5e38\u9002\u5408\u4e0b\u8f7d\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4e0b\u8f7d\u89c6\u9891<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def download_video(url, output_path):<\/p>\n<p>    response = requests.get(url, stream=True)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        with open(output_path, &#39;wb&#39;) as file:<\/p>\n<p>            for chunk in response.iter_content(chunk_size=1024):<\/p>\n<p>                if chunk:<\/p>\n<p>                    file.write(chunk)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to download file. Status code: {response.status_code}&quot;)<\/p>\n<p>url = &quot;https:\/\/example.com\/video.mp4&quot;<\/p>\n<p>output_path = &quot;video.mp4&quot;<\/p>\n<p>download_video(url, output_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    file_size = os.path.getsize(file_path)<\/p>\n<p>    return file_size<\/p>\n<p>file_size = get_file_size(output_path)<\/p>\n<p>print(f&quot;File size: {file_size} bytes&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>urllib<\/code> \u5e93\u4e0b\u8f7d\u89c6\u9891\u5e76\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/h3>\n<\/p>\n<p><p><code>urllib<\/code> \u662f Python \u6807\u51c6\u5e93\u4e2d\u7684\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406 URL\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u4e0b\u8f7d\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4e0b\u8f7d\u89c6\u9891<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.request<\/p>\n<p>def download_video(url, output_path):<\/p>\n<p>    urllib.request.urlretrieve(url, output_path)<\/p>\n<p>url = &quot;https:\/\/example.com\/video.mp4&quot;<\/p>\n<p>output_path = &quot;video.mp4&quot;<\/p>\n<p>download_video(url, output_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_size = os.path.getsize(output_path)<\/p>\n<p>print(f&quot;File size: {file_size} bytes&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>wget<\/code> \u5e93\u4e0b\u8f7d\u89c6\u9891\u5e76\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/h3>\n<\/p>\n<p><p><code>wget<\/code> \u662f\u4e00\u4e2a Python \u5305\u88c5\u5668\uff0c\u7528\u4e8e\u8c03\u7528 GNU Wget \u5de5\u5177\u8fdb\u884c\u6587\u4ef6\u4e0b\u8f7d\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5 <code>wget<\/code><\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install wget<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4e0b\u8f7d\u89c6\u9891<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import wget<\/p>\n<p>url = &quot;https:\/\/example.com\/video.mp4&quot;<\/p>\n<p>output_path = wget.download(url)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_size = os.path.getsize(output_path)<\/p>\n<p>print(f&quot;File size: {file_size} bytes&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5f02\u5e38\u548c\u8fdb\u5ea6\u6761<\/h3>\n<\/p>\n<p><p>\u5728\u4e0b\u8f7d\u6587\u4ef6\u65f6\u5904\u7406\u5f02\u5e38\u548c\u663e\u793a\u8fdb\u5ea6\u6761\u53ef\u4ee5\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u5f02\u5e38<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def download_video(url, output_path):<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url, stream=True)<\/p>\n<p>        response.raise_for_status()  # Raise HTTPError for bad responses<\/p>\n<p>        with open(output_path, &#39;wb&#39;) as file:<\/p>\n<p>            for chunk in response.iter_content(chunk_size=1024):<\/p>\n<p>                if chunk:<\/p>\n<p>                    file.write(chunk)<\/p>\n<p>    except requests.exceptions.RequestException as e:<\/p>\n<p>        print(f&quot;Failed to download file: {e}&quot;)<\/p>\n<p>url = &quot;https:\/\/example.com\/video.mp4&quot;<\/p>\n<p>output_path = &quot;video.mp4&quot;<\/p>\n<p>download_video(url, output_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u663e\u793a\u8fdb\u5ea6\u6761<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from tqdm import tqdm<\/p>\n<p>def download_video(url, output_path):<\/p>\n<p>    response = requests.get(url, stream=True)<\/p>\n<p>    total_size = int(response.headers.get(&#39;content-length&#39;, 0))<\/p>\n<p>    with open(output_path, &#39;wb&#39;) as file, tqdm(<\/p>\n<p>        desc=output_path,<\/p>\n<p>        total=total_size,<\/p>\n<p>        unit=&#39;iB&#39;,<\/p>\n<p>        unit_scale=True,<\/p>\n<p>        unit_divisor=1024,<\/p>\n<p>    ) as bar:<\/p>\n<p>        for chunk in response.iter_content(chunk_size=1024):<\/p>\n<p>            size = file.write(chunk)<\/p>\n<p>            bar.update(size)<\/p>\n<p>url = &quot;https:\/\/example.com\/video.mp4&quot;<\/p>\n<p>output_path = &quot;video.mp4&quot;<\/p>\n<p>download_video(url, output_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Python \u8f7b\u677e\u4e0b\u8f7d\u89c6\u9891\u6587\u4ef6\u5e76\u83b7\u53d6\u5176\u5927\u5c0f\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u5e93\uff08\u5982 <code>requests<\/code>\u3001<code>urllib<\/code>\u3001<code>wget<\/code>\uff09\u3001\u5904\u7406\u5f02\u5e38\u548c\u663e\u793a\u8fdb\u5ea6\u6761<\/strong>\u662f\u786e\u4fdd\u4e0b\u8f7d\u8fc7\u7a0b\u987a\u5229\u8fdb\u884c\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u5e0c\u671b\u672c\u6587\u80fd\u4e3a\u60a8\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u548c\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u548c\u793a\u4f8b\u4ee3\u7801\u8986\u76d6\u4e86\u4ece\u4e0b\u8f7d\u89c6\u9891\u6587\u4ef6\u5230\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u7684\u5404\u4e2a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6848\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u89c6\u9891\u6587\u4ef6\u7684\u5927\u5c0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u901a\u8fc7<code>os.path.getsize()<\/code>\u51fd\u6570\uff0c\u60a8\u53ea\u9700\u4f20\u5165\u89c6\u9891\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u5373\u53ef\u8fd4\u56de\u5176\u5b57\u8282\u5927\u5c0f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;path\/to\/your\/video.mp4&#39;\nfile_size = os.path.getsize(file_path)\nprint(f&quot;\u89c6\u9891\u6587\u4ef6\u5927\u5c0f: {file_size} \u5b57\u8282&quot;)\n<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u672c\u5730\u5b58\u50a8\u7684\u89c6\u9891\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u4e0b\u8f7d\u89c6\u9891\u65f6\uff0c\u5982\u4f55<a href=\"https:\/\/docs.pingcode.com\/agile\/project-management\/estimation\" target=\"_blank\">\u4f30\u7b97<\/a>\u6587\u4ef6\u7684\u5927\u5c0f\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u60f3\u5728\u4e0b\u8f7d\u89c6\u9891\u4e4b\u524d\u4f30\u7b97\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bf7\u6c42\u89c6\u9891\u6587\u4ef6\u7684HTTP\u5934\u4fe1\u606f\u6765\u83b7\u53d6\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u53d1\u9001HEAD\u8bf7\u6c42\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\n\nurl = &#39;http:\/\/example.com\/video.mp4&#39;\nresponse = requests.head(url)\nfile_size = response.headers.get(&#39;content-length&#39;, 0)\nprint(f&quot;\u4f30\u7b97\u7684\u89c6\u9891\u6587\u4ef6\u5927\u5c0f: {file_size} \u5b57\u8282&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728\u4e0b\u8f7d\u4e4b\u524d\u4e86\u89e3\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n<p><strong>\u4e0b\u8f7d\u89c6\u9891\u65f6\u5982\u4f55\u5904\u7406\u5927\u6587\u4ef6\u4ee5\u907f\u514d\u5185\u5b58\u6ea2\u51fa\uff1f<\/strong><br \/>\u4e0b\u8f7d\u5927\u89c6\u9891\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6d41\u5f0f\u4e0b\u8f7d\u65b9\u5f0f\u6765\u907f\u514d\u5185\u5b58\u6ea2\u51fa\u3002\u5728\u4f7f\u7528<code>requests<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>stream=True<\/code>\u6765\u9010\u5757\u4e0b\u8f7d\u6587\u4ef6\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\n\nurl = &#39;http:\/\/example.com\/video.mp4&#39;\nresponse = requests.get(url, stream=True)\n\nwith open(&#39;video.mp4&#39;, &#39;wb&#39;) as file:\n    for chunk in response.iter_content(chunk_size=8192):\n        file.write(chunk)\nprint(&quot;\u89c6\u9891\u6587\u4ef6\u4e0b\u8f7d\u5b8c\u6210&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u9010\u6b65\u5199\u5165\u6587\u4ef6\uff0c\u4ece\u800c\u6709\u6548\u7ba1\u7406\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u4e0b\u8f7d\u89c6\u9891\u6587\u4ef6\u5927\u5c0f \u5728 Python \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u548c\u65b9\u6cd5\u6765\u4e0b\u8f7d\u89c6\u9891\u6587\u4ef6\u5e76\u83b7\u53d6\u5176\u5927\u5c0f\u3002\u4f7f\u7528\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1036130,"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\/1036121"}],"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=1036121"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036121\/revisions"}],"predecessor-version":[{"id":1036134,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036121\/revisions\/1036134"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1036130"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1036121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1036121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1036121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}