{"id":33677,"date":"2025-04-28T10:46:59","date_gmt":"2025-04-28T02:46:59","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/33677.html"},"modified":"2025-04-28T10:46:59","modified_gmt":"2025-04-28T02:46:59","slug":"python-%e8%a7%86%e9%a2%91%e7%88%ac%e5%8f%96%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/33677.html","title":{"rendered":"Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u524d\u8a00<\/li>\n<li>\u57fa\u672c\u539f\u7406<\/li>\n<li>\u73af\u5883\u51c6\u5907<\/li>\n<li>\n<ul>\n<li>Python\u5b89\u88c5<\/li>\n<li>\u9009\u62e9Python\u5f00\u53d1\u73af\u5883<\/li>\n<li>\u5b89\u88c5\u5fc5\u8981\u5e93<\/li>\n<\/ul>\n<\/li>\n<li>\u793a\u4f8b 1&#xff1a;\u722c\u53d6\u7b80\u5355\u76f4\u94fe\u89c6\u9891<\/li>\n<li>\u793a\u4f8b 2&#xff1a;\u722c\u53d6\u57fa\u4e8e HTML5 \u7684\u89c6\u9891&#xff08;\u4ee5\u67d0\u7b80\u5355\u89c6\u9891\u7f51\u7ad9\u4e3a\u4f8b&#xff09;<\/li>\n<\/ul>\n<hr \/>\n<h2>\u524d\u8a00<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8f83\u4e3a\u5b8c\u6574\u7684 Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b&#xff0c;\u5305\u542b\u57fa\u672c\u539f\u7406\u3001\u4e0d\u540c\u7f51\u7ad9\u7684\u722c\u53d6\u793a\u4f8b\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<hr \/>\n<h2>\u57fa\u672c\u539f\u7406<\/h2>\n<p>\u89c6\u9891\u722c\u53d6\u672c\u8d28\u4e0a\u662f\u6a21\u62df\u6d4f\u89c8\u5668\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42&#xff0c;\u83b7\u53d6\u5305\u542b\u89c6\u9891\u4fe1\u606f\u7684\u7f51\u9875\u5185\u5bb9&#xff0c;\u89e3\u6790\u51fa\u89c6\u9891\u7684\u771f\u5b9e\u4e0b\u8f7d\u5730\u5740&#xff0c;\u7136\u540e\u5c06\u89c6\u9891\u6587\u4ef6\u4e0b\u8f7d\u5230\u672c\u5730\u3002\u4e00\u822c\u4f1a\u7528\u5230requests\u5e93\u6765\u53d1\u9001\u8bf7\u6c42&#xff0c;BeautifulSoup\u6216lxml\u5e93\u6765\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<h2>\u73af\u5883\u51c6\u5907<\/h2>\n<h3>Python\u5b89\u88c5<\/h3>\n<p>\u8bbf\u95ee Python \u5b98\u65b9\u7f51\u7ad9&#xff0c;\u6839\u636e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf&#xff08;Windows\u3001Mac \u6216 Linux&#xff09;\u4e0b\u8f7d\u5e76\u5b89\u88c5 Python 3.x \u7248\u672c\u3002\u5b89\u88c5\u65f6\u52fe\u9009 \u201cAdd Python to PATH\u201d&#xff0c;\u65b9\u4fbf\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528 Python\u3002<\/p>\n<p>Python 3.11\u5b89\u88c5\u6559\u7a0b&#xff1a;https:\/\/blog.csdn.net\/u014164303\/article\/details\/145549489 Python 3.13\u5b89\u88c5\u6559\u7a0b&#xff1a;https:\/\/blog.csdn.net\/u014164303\/article\/details\/146024500<\/p>\n<p>Python 3.11\u4e0b\u8f7d\u5730\u5740&#xff1a;https:\/\/pan.quark.cn\/s\/9c44793cb24c Python 3.13\u4e0b\u8f7d\u5730\u5740&#xff1a;https:\/\/pan.quark.cn\/s\/bce37ebd7f70<\/p>\n<h3>\u9009\u62e9Python\u5f00\u53d1\u73af\u5883<\/h3>\n<p>PyCharm \u793e\u533a\u7248&#xff08;\u514d\u8d39&#xff09;\u6216\u4e13\u4e1a\u7248&#xff08;\u9700\u4ed8\u8d39\u6216\u7533\u8bf7\u6559\u80b2\u7248&#xff09;\u3002\u5b89\u88c5\u5b8c\u6210\u540e&#xff0c;\u6253\u5f00 PyCharm&#xff0c;\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee&#xff0c;\u5728\u9879\u76ee\u8bbe\u7f6e\u4e2d\u9009\u62e9\u4e4b\u524d\u521b\u5efa\u7684\u865a\u62df\u73af\u5883\u4f5c\u4e3a\u9879\u76ee\u7684 Python \u89e3\u91ca\u5668\u3002PyCharm \u529f\u80fd\u5f3a\u5927&#xff0c;\u63d0\u4f9b\u4ee3\u7801\u81ea\u52a8\u8865\u5168\u3001\u8c03\u8bd5\u7b49\u529f\u80fd&#xff0c;\u9002\u5408\u5f00\u53d1\u5927\u578b\u9879\u76ee\u3002<\/p>\n<p>Pycharm\u5b89\u88c5\u6559\u7a0b&#xff1a;https:\/\/blog.csdn.net\/u014164303\/article\/details\/145674773 PyCharm\u4e0b\u8f7d\u5730\u5740&#xff1a;https:\/\/pan.quark.cn\/s\/5756c8cf8b2a<\/p>\n<h3>\u5b89\u88c5\u5fc5\u8981\u5e93<\/h3>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d&#xff0c;\u9700\u8981\u5b89\u88c5\u5fc5\u8981\u7684 Python \u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5&#xff1a;<\/p>\n<p>pip install requests beautifulsoup4 lxml<\/p>\n<h2>\u793a\u4f8b 1&#xff1a;\u722c\u53d6\u7b80\u5355\u76f4\u94fe\u89c6\u9891<\/h2>\n<p>\u6709\u4e9b\u7f51\u7ad9\u4f1a\u76f4\u63a5\u63d0\u4f9b\u89c6\u9891\u7684\u4e0b\u8f7d\u94fe\u63a5&#xff0c;\u5bf9\u4e8e\u8fd9\u7c7b\u89c6\u9891&#xff0c;\u722c\u53d6\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>import requests<\/p>\n<p>def <span class=\"token function\">download_video<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">,<\/span> save_path<span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><br \/>\n    try<span class=\"token operator\">:<\/span><br \/>\n        # \u53d1\u9001\u8bf7\u6c42&#xff0c;\u83b7\u53d6\u89c6\u9891\u5185\u5bb9<br \/>\n        response <span class=\"token operator\">&#061;<\/span> requests<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">,<\/span> stream<span class=\"token operator\">&#061;<\/span>True<span class=\"token punctuation\">)<\/span><br \/>\n        response<span class=\"token punctuation\">.<\/span><span class=\"token function\">raise_for_status<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        # \u4ee5\u4e8c\u8fdb\u5236\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6<br \/>\n        with <span class=\"token function\">open<\/span><span class=\"token punctuation\">(<\/span>save_path<span class=\"token punctuation\">,<\/span> <span class=\"token char\">&#039;wb&#039;<\/span><span class=\"token punctuation\">)<\/span> as file<span class=\"token operator\">:<\/span><br \/>\n            <span class=\"token keyword\">for<\/span> chunk in response<span class=\"token punctuation\">.<\/span><span class=\"token function\">iter_content<\/span><span class=\"token punctuation\">(<\/span>chunk_size<span class=\"token operator\">&#061;<\/span><span class=\"token number\">8192<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> chunk<span class=\"token operator\">:<\/span><br \/>\n                    file<span class=\"token punctuation\">.<\/span><span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>chunk<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token function\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token string\">&#034;\u89c6\u9891\u4e0b\u8f7d\u6210\u529f&#xff0c;\u4fdd\u5b58\u8def\u5f84&#xff1a;{save_path}&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    except requests<span class=\"token punctuation\">.<\/span>RequestException as e<span class=\"token operator\">:<\/span><br \/>\n        <span class=\"token function\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token string\">&#034;\u4e0b\u8f7d\u5931\u8d25&#xff1a;{e}&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p># \u89c6\u9891\u7684\u76f4\u63a5\u4e0b\u8f7d\u94fe\u63a5<br \/>\nvideo_url <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;https:\/\/example.com\/video.mp4&#034;<\/span><br \/>\n# \u4fdd\u5b58\u89c6\u9891\u7684\u672c\u5730\u8def\u5f84<br \/>\nsave_path <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;video.mp4&#034;<\/span><\/p>\n<p><span class=\"token function\">download_video<\/span><span class=\"token punctuation\">(<\/span>video_url<span class=\"token punctuation\">,<\/span> save_path<span class=\"token punctuation\">)<\/span><\/p>\n<h2>\u793a\u4f8b 2&#xff1a;\u722c\u53d6\u57fa\u4e8e HTML5 \u7684\u89c6\u9891&#xff08;\u4ee5\u67d0\u7b80\u5355\u89c6\u9891\u7f51\u7ad9\u4e3a\u4f8b&#xff09;<\/h2>\n<p>\u6709\u4e9b\u7f51\u7ad9\u4f7f\u7528 HTML5 \u7684\u6807\u7b7e\u6765\u64ad\u653e\u89c6\u9891&#xff0c;\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u83b7\u53d6\u89c6\u9891\u7684\u771f\u5b9e\u5730\u5740\u3002<\/p>\n<p>import requests<br \/>\nfrom bs4 import BeautifulSoup<\/p>\n<p>def <span class=\"token function\">get_video_url<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><br \/>\n    try<span class=\"token operator\">:<\/span><br \/>\n        # \u53d1\u9001\u8bf7\u6c42&#xff0c;\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<br \/>\n        response <span class=\"token operator\">&#061;<\/span> requests<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">)<\/span><br \/>\n        response<span class=\"token punctuation\">.<\/span><span class=\"token function\">raise_for_status<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        # \u4f7f\u7528 BeautifulSoup \u89e3\u6790\u7f51\u9875<br \/>\n        soup <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">BeautifulSoup<\/span><span class=\"token punctuation\">(<\/span>response<span class=\"token punctuation\">.<\/span>text<span class=\"token punctuation\">,<\/span> <span class=\"token char\">&#039;lxml&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        # \u67e5\u627e video \u6807\u7b7e<br \/>\n        video_tag <span class=\"token operator\">&#061;<\/span> soup<span class=\"token punctuation\">.<\/span><span class=\"token function\">find<\/span><span class=\"token punctuation\">(<\/span><span class=\"token char\">&#039;video&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> video_tag<span class=\"token operator\">:<\/span><br \/>\n            # \u83b7\u53d6\u89c6\u9891\u7684 src \u5c5e\u6027<br \/>\n            video_url <span class=\"token operator\">&#061;<\/span> video_tag<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token char\">&#039;src&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> video_url<span class=\"token operator\">:<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> not video_url<span class=\"token punctuation\">.<\/span><span class=\"token function\">startswith<\/span><span class=\"token punctuation\">(<\/span><span class=\"token char\">&#039;http&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><br \/>\n                    # \u5904\u7406\u76f8\u5bf9\u8def\u5f84<br \/>\n                    base_url <span class=\"token operator\">&#061;<\/span> url<span class=\"token punctuation\">.<\/span><span class=\"token function\">rsplit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token char\">&#039;\/&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><br \/>\n                    video_url <span class=\"token operator\">&#061;<\/span> f<span class=\"token string\">&#034;{base_url}\/{video_url}&#034;<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> video_url<br \/>\n    except requests<span class=\"token punctuation\">.<\/span>RequestException as e<span class=\"token operator\">:<\/span><br \/>\n        <span class=\"token function\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token string\">&#034;\u8bf7\u6c42\u5931\u8d25&#xff1a;{e}&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> None<\/p>\n<p>def <span class=\"token function\">download_video<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">,<\/span> save_path<span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><br \/>\n    try<span class=\"token operator\">:<\/span><br \/>\n        response <span class=\"token operator\">&#061;<\/span> requests<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">,<\/span> stream<span class=\"token operator\">&#061;<\/span>True<span class=\"token punctuation\">)<\/span><br \/>\n        response<span class=\"token punctuation\">.<\/span><span class=\"token function\">raise_for_status<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        with <span class=\"token function\">open<\/span><span class=\"token punctuation\">(<\/span>save_path<span class=\"token punctuation\">,<\/span> <span class=\"token char\">&#039;wb&#039;<\/span><span class=\"token punctuation\">)<\/span> as file<span class=\"token operator\">:<\/span><br \/>\n            <span class=\"token keyword\">for<\/span> chunk in response<span class=\"token punctuation\">.<\/span><span class=\"token function\">iter_content<\/span><span class=\"token punctuation\">(<\/span>chunk_size<span class=\"token operator\">&#061;<\/span><span class=\"token number\">8192<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> chunk<span class=\"token operator\">:<\/span><br \/>\n                    file<span class=\"token punctuation\">.<\/span><span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>chunk<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token function\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token string\">&#034;\u89c6\u9891\u4e0b\u8f7d\u6210\u529f&#xff0c;\u4fdd\u5b58\u8def\u5f84&#xff1a;{save_path}&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    except requests<span class=\"token punctuation\">.<\/span>RequestException as e<span class=\"token operator\">:<\/span><br \/>\n        <span class=\"token function\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token string\">&#034;\u4e0b\u8f7d\u5931\u8d25&#xff1a;{e}&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p># \u89c6\u9891\u7f51\u9875\u7684 URL<br \/>\nweb_url <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;https:\/\/example.com\/video_page.html&#034;<\/span><br \/>\n# \u83b7\u53d6\u89c6\u9891\u7684\u771f\u5b9e\u4e0b\u8f7d\u5730\u5740<br \/>\nvideo_url <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">get_video_url<\/span><span class=\"token punctuation\">(<\/span>web_url<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">if<\/span> video_url<span class=\"token operator\">:<\/span><br \/>\n    save_path <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;video.mp4&#034;<\/span><br \/>\n    <span class=\"token function\">download_video<\/span><span class=\"token punctuation\">(<\/span>video_url<span class=\"token punctuation\">,<\/span> save_path<span class=\"token punctuation\">)<\/span><br \/>\n\u793a\u4f8b <span class=\"token number\">3<\/span>&#xff1a;\u722c\u53d6\u77ed\u89c6\u9891\u5e73\u53f0\u89c6\u9891&#xff08;\u4ee5\u6296\u97f3\u4e3a\u4f8b&#xff09;<br \/>\n\u6296\u97f3\u7684\u89c6\u9891\u722c\u53d6\u76f8\u5bf9\u590d\u6742&#xff0c;\u56e0\u4e3a\u5176\u91c7\u7528\u4e86\u52a0\u5bc6\u548c\u53cd\u722c\u673a\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b&#xff1a;<br \/>\npython<br \/>\nimport requests<br \/>\nimport re<\/p>\n<p>def <span class=\"token function\">get_douyin_video_url<\/span><span class=\"token punctuation\">(<\/span>share_url<span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><br \/>\n    try<span class=\"token operator\">:<\/span><br \/>\n        # \u53d1\u9001\u8bf7\u6c42&#xff0c;\u83b7\u53d6\u91cd\u5b9a\u5411\u540e\u7684 URL<br \/>\n        response <span class=\"token operator\">&#061;<\/span> requests<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>share_url<span class=\"token punctuation\">,<\/span> allow_redirects<span class=\"token operator\">&#061;<\/span>True<span class=\"token punctuation\">)<\/span><br \/>\n        response<span class=\"token punctuation\">.<\/span><span class=\"token function\">raise_for_status<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u89c6\u9891 ID<br \/>\n        video_id_pattern <span class=\"token operator\">&#061;<\/span> re<span class=\"token punctuation\">.<\/span><span class=\"token function\">compile<\/span><span class=\"token punctuation\">(<\/span>r<span class=\"token char\">&#039;video\/(\\\\d&#043;)&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        match <span class=\"token operator\">&#061;<\/span> video_id_pattern<span class=\"token punctuation\">.<\/span><span class=\"token function\">search<\/span><span class=\"token punctuation\">(<\/span>response<span class=\"token punctuation\">.<\/span>url<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> match<span class=\"token operator\">:<\/span><br \/>\n            video_id <span class=\"token operator\">&#061;<\/span> match<span class=\"token punctuation\">.<\/span><span class=\"token function\">group<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            # \u6784\u9020\u89c6\u9891 API \u8bf7\u6c42 URL<br \/>\n            api_url <span class=\"token operator\">&#061;<\/span> f<span class=\"token string\">&#034;https:\/\/www.iesdouyin.com\/web\/api\/v2\/video\/playwm\/?video_id&#061;{video_id}&#034;<\/span><br \/>\n            headers <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token string\">&#034;User-Agent&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#034;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n            # \u53d1\u9001 API \u8bf7\u6c42&#xff0c;\u83b7\u53d6\u89c6\u9891\u4fe1\u606f<br \/>\n            api_response <span class=\"token operator\">&#061;<\/span> requests<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>api_url<span class=\"token punctuation\">,<\/span> headers<span class=\"token operator\">&#061;<\/span>headers<span class=\"token punctuation\">)<\/span><br \/>\n            api_response<span class=\"token punctuation\">.<\/span><span class=\"token function\">raise_for_status<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            data <span class=\"token operator\">&#061;<\/span> api_response<span class=\"token punctuation\">.<\/span><span class=\"token function\">json<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> <span class=\"token char\">&#039;video&#039;<\/span> in data and <span class=\"token char\">&#039;play_addr&#039;<\/span> in data<span class=\"token punctuation\">[<\/span><span class=\"token char\">&#039;video&#039;<\/span><span class=\"token punctuation\">]<\/span> and <span class=\"token char\">&#039;url_list&#039;<\/span> in data<span class=\"token punctuation\">[<\/span><span class=\"token char\">&#039;video&#039;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">[<\/span><span class=\"token char\">&#039;play_addr&#039;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">:<\/span><br \/>\n                video_url <span class=\"token operator\">&#061;<\/span> data<span class=\"token punctuation\">[<\/span><span class=\"token char\">&#039;video&#039;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">[<\/span><span class=\"token char\">&#039;play_addr&#039;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">[<\/span><span class=\"token char\">&#039;url_list&#039;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><br \/>\n                # \u53bb\u9664\u6c34\u5370<br \/>\n                video_url <span class=\"token operator\">&#061;<\/span> video_url<span class=\"token punctuation\">.<\/span><span class=\"token function\">replace<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;playwm&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;play&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n                <span class=\"token keyword\">return<\/span> video_url<br \/>\n    except requests<span class=\"token punctuation\">.<\/span>RequestException as e<span class=\"token operator\">:<\/span><br \/>\n        <span class=\"token function\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token string\">&#034;\u8bf7\u6c42\u5931\u8d25&#xff1a;{e}&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> None<\/p>\n<p>def <span class=\"token function\">download_video<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">,<\/span> save_path<span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><br \/>\n    try<span class=\"token operator\">:<\/span><br \/>\n        response <span class=\"token operator\">&#061;<\/span> requests<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>url<span class=\"token punctuation\">,<\/span> stream<span class=\"token operator\">&#061;<\/span>True<span class=\"token punctuation\">)<\/span><br \/>\n        response<span class=\"token punctuation\">.<\/span><span class=\"token function\">raise_for_status<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        with <span class=\"token function\">open<\/span><span class=\"token punctuation\">(<\/span>save_path<span class=\"token punctuation\">,<\/span> <span class=\"token char\">&#039;wb&#039;<\/span><span class=\"token punctuation\">)<\/span> as file<span class=\"token operator\">:<\/span><br \/>\n            <span class=\"token keyword\">for<\/span> chunk in response<span class=\"token punctuation\">.<\/span><span class=\"token function\">iter_content<\/span><span class=\"token punctuation\">(<\/span>chunk_size<span class=\"token operator\">&#061;<\/span><span class=\"token number\">8192<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> chunk<span class=\"token operator\">:<\/span><br \/>\n                    file<span class=\"token punctuation\">.<\/span><span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>chunk<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token function\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token string\">&#034;\u89c6\u9891\u4e0b\u8f7d\u6210\u529f&#xff0c;\u4fdd\u5b58\u8def\u5f84&#xff1a;{save_path}&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    except requests<span class=\"token punctuation\">.<\/span>RequestException as e<span class=\"token operator\">:<\/span><br \/>\n        <span class=\"token function\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token string\">&#034;\u4e0b\u8f7d\u5931\u8d25&#xff1a;{e}&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p># \u6296\u97f3\u89c6\u9891\u7684\u5206\u4eab\u94fe\u63a5<br \/>\nshare_url <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;https:\/\/v.douyin.com\/xxxxxx\/&#034;<\/span><br \/>\n# \u83b7\u53d6\u89c6\u9891\u7684\u771f\u5b9e\u4e0b\u8f7d\u5730\u5740<br \/>\nvideo_url <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">get_douyin_video_url<\/span><span class=\"token punctuation\">(<\/span>share_url<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">if<\/span> video_url<span class=\"token operator\">:<\/span><br \/>\n    save_path <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;douyin_video.mp4&#034;<\/span><br \/>\n    <span class=\"token function\">download_video<\/span><span class=\"token punctuation\">(<\/span>video_url<span class=\"token punctuation\">,<\/span> save_path<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u6ce8\u610f\u4e8b\u9879<\/p>\n<ul>\n<li>\u9075\u5b88\u6cd5\u5f8b\u6cd5\u89c4&#xff1a;\u5728\u8fdb\u884c\u89c6\u9891\u722c\u53d6\u65f6&#xff0c;\u5fc5\u987b\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u548c\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e&#xff0c;\u4e0d\u5f97\u8fdb\u884c\u975e\u6cd5\u722c\u53d6\u548c\u4f20\u64ad\u3002<\/li>\n<li>\u53cd\u722c\u673a\u5236&#xff1a;\u8bb8\u591a\u7f51\u7ad9\u90fd\u6709\u53cd\u722c\u673a\u5236&#xff0c;\u5982 IP \u5c01\u7981\u3001\u9a8c\u8bc1\u7801\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7406 IP\u3001\u8bbe\u7f6e\u5408\u7406\u7684\u8bf7\u6c42\u5934\u548c\u8bf7\u6c42\u95f4\u9694\u65f6\u95f4\u6765\u7ed5\u8fc7\u53cd\u722c\u673a\u5236\u3002<\/li>\n<li>\u7248\u6743\u95ee\u9898&#xff1a;\u786e\u4fdd\u4e0b\u8f7d\u7684\u89c6\u9891\u5177\u6709\u5408\u6cd5\u7684\u4f7f\u7528\u6743\u9650&#xff0c;\u907f\u514d\u4fb5\u72af\u4ed6\u4eba\u7684\u7248\u6743\u3002 \u901a\u8fc7\u4ee5\u4e0a\u6559\u7a0b&#xff0c;\u4f60\u53ef\u4ee5\u4e86\u89e3\u5230\u4e0d\u540c\u573a\u666f\u4e0b Python \u89c6\u9891\u722c\u53d6\u7684\u57fa\u672c\u65b9\u6cd5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d&#xff0c;\u9700\u8981\u6839\u636e\u5177\u4f53\u7f51\u7ad9\u7684\u7279\u70b9\u8fdb\u884c\u8c03\u6574\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb5.5k\u6b21\uff0c\u70b9\u8d5e43\u6b21\uff0c\u6536\u85cf75\u6b21\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8f83\u4e3a\u5b8c\u6574\u7684 Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b\uff0c\u5305\u542b\u57fa\u672c\u539f\u7406\u3001\u4e0d\u540c\u7f51\u7ad9\u7684\u722c\u53d6\u793a\u4f8b\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u3002_csdn python \u722c\u53d6\u89c6\u9891<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,2797,2795,2794,2796],"topic":[],"class_list":["post-33677","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-python-"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/33677.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb5.5k\u6b21\uff0c\u70b9\u8d5e43\u6b21\uff0c\u6536\u85cf75\u6b21\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8f83\u4e3a\u5b8c\u6574\u7684 Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b\uff0c\u5305\u542b\u57fa\u672c\u539f\u7406\u3001\u4e0d\u540c\u7f51\u7ad9\u7684\u722c\u53d6\u793a\u4f8b\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u3002_csdn python \u722c\u53d6\u89c6\u9891\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/33677.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-28T02:46:59+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/33677.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/33677.html\",\"name\":\"Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-28T02:46:59+00:00\",\"dateModified\":\"2025-04-28T02:46:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/33677.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/33677.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/33677.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/33677.html","og_locale":"zh_CN","og_type":"article","og_title":"Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb5.5k\u6b21\uff0c\u70b9\u8d5e43\u6b21\uff0c\u6536\u85cf75\u6b21\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8f83\u4e3a\u5b8c\u6574\u7684 Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b\uff0c\u5305\u542b\u57fa\u672c\u539f\u7406\u3001\u4e0d\u540c\u7f51\u7ad9\u7684\u722c\u53d6\u793a\u4f8b\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u3002_csdn python \u722c\u53d6\u89c6\u9891","og_url":"https:\/\/www.wsisp.com\/helps\/33677.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-28T02:46:59+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/33677.html","url":"https:\/\/www.wsisp.com\/helps\/33677.html","name":"Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-28T02:46:59+00:00","dateModified":"2025-04-28T02:46:59+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/33677.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/33677.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/33677.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python \u89c6\u9891\u722c\u53d6\u6559\u7a0b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/33677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=33677"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/33677\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=33677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=33677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=33677"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=33677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}