{"id":1159926,"date":"2025-01-13T18:58:46","date_gmt":"2025-01-13T10:58:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159926.html"},"modified":"2025-01-13T18:58:49","modified_gmt":"2025-01-13T10:58:49","slug":"python%e5%a6%82%e4%bd%95%e7%88%ac%e5%8f%96%e7%bd%91%e7%ab%99%e8%a7%86%e9%a2%91","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1159926.html","title":{"rendered":"python\u5982\u4f55\u722c\u53d6\u7f51\u7ad9\u89c6\u9891"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201532\/246960b5-00fa-4e7f-91f3-bee846647f08.webp\" alt=\"python\u5982\u4f55\u722c\u53d6\u7f51\u7ad9\u89c6\u9891\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u722c\u53d6\u7f51\u7ad9\u89c6\u9891\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u7f51\u7edc\u8bf7\u6c42\u5e93\u3001\u89e3\u6790HTML\u5185\u5bb9\u7684\u5e93\u548c\u89c6\u9891\u4e0b\u8f7d\u5de5\u5177\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790\u89c6\u9891\u94fe\u63a5\u3001\u4f7f\u7528youtube-dl\u4e0b\u8f7d\u89c6\u9891<\/strong>\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528<code>youtube-dl<\/code>\u5e93\u4e0b\u8f7d\u89c6\u9891\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u722c\u53d6\u7f51\u7ad9\u89c6\u9891\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9996\u5148\u9700\u8981\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>requests<\/code>\u5e93\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/example.com\/video-page&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>html_content = response.content<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests.get()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728<code>html_content<\/code>\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u89e3\u6790HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u540e\uff0c\u6211\u4eec\u9700\u8981\u89e3\u6790HTML\u5185\u5bb9\u4ee5\u627e\u5230\u89c6\u9891\u94fe\u63a5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p>video_tag = soup.find(&#39;video&#39;)<\/p>\n<p>video_url = video_tag[&#39;src&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>BeautifulSoup<\/code>\u89e3\u6790HTML\u5185\u5bb9\uff0c\u5e76\u627e\u5230<code>&lt;video&gt;<\/code>\u6807\u7b7e\u3002\u7136\u540e\uff0c\u4ece\u8be5\u6807\u7b7e\u4e2d\u63d0\u53d6\u89c6\u9891\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528youtube-dl\u4e0b\u8f7d\u89c6\u9891<\/h3>\n<\/p>\n<p><p>\u4e0b\u8f7d\u89c6\u9891\u662f\u722c\u53d6\u7f51\u7ad9\u89c6\u9891\u7684\u6700\u540e\u4e00\u6b65\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>youtube-dl<\/code>\u5e93\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import youtube_dl<\/p>\n<p>ydl_opts = {}<\/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>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a<code>youtube_dl.YoutubeDL<\/code>\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>download()<\/code>\u65b9\u6cd5\u4e0b\u8f7d\u89c6\u9891\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u7ad9\u7684\u89c6\u9891\u94fe\u63a5\u662f\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u7684\uff0c\u8fd9\u79cd\u60c5\u51b5\u6211\u4eec\u9700\u8981\u4f7f\u7528<code>selenium<\/code>\u5e93\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\u3002<\/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.chrome.service import Service<\/p>\n<p>from webdriver_manager.chrome import ChromeDriverManager<\/p>\n<p>driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))<\/p>\n<p>driver.get(&#39;https:\/\/example.com\/video-page&#39;)<\/p>\n<p>video_element = driver.find_element(By.TAG_NAME, &#39;video&#39;)<\/p>\n<p>video_url = video_element.get_attribute(&#39;src&#39;)<\/p>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>selenium<\/code>\u5e93\u6253\u5f00\u7f51\u9875\uff0c\u5e76\u627e\u5230<code>&lt;video&gt;<\/code>\u6807\u7b7e\u3002\u7136\u540e\uff0c\u4ece\u8be5\u6807\u7b7e\u4e2d\u63d0\u53d6\u89c6\u9891\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u53cd\u722c\u866b\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u8bb8\u591a\u7f51\u7ad9\u5177\u6709\u53cd\u722c\u866b\u673a\u5236\uff0c\u8fd9\u53ef\u80fd\u4f1a\u963b\u6b62\u60a8\u722c\u53d6\u89c6\u9891\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6280\u672f\u6765\u7ed5\u8fc7\u53cd\u722c\u866b\u673a\u5236\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f2a\u88c5\u8bf7\u6c42\u5934<\/strong>\uff1a\u901a\u8fc7\u4f2a\u88c5\u8bf7\u6c42\u5934\uff0c\u53ef\u4ee5\u6a21\u62df\u771f\u5b9e\u7528\u6237\u7684\u8bf7\u6c42\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">headers = {<\/p>\n<p>    &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u4ee3\u7406<\/strong>\uff1a\u901a\u8fc7\u4f7f\u7528\u4ee3\u7406\uff0c\u53ef\u4ee5\u9690\u85cf\u60a8\u7684\u771f\u5b9eIP\u5730\u5740\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">proxies = {<\/p>\n<p>    &#39;http&#39;: &#39;http:\/\/your_proxy:port&#39;,<\/p>\n<p>    &#39;https&#39;: &#39;https:\/\/your_proxy:port&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers, proxies=proxies)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6a21\u62df\u7528\u6237\u884c\u4e3a<\/strong>\uff1a\u901a\u8fc7\u6a21\u62df\u7528\u6237\u884c\u4e3a\uff0c\u53ef\u4ee5\u907f\u514d\u89e6\u53d1\u53cd\u722c\u866b\u673a\u5236\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.action_ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns import ActionChains<\/p>\n<p>actions = ActionChains(driver)<\/p>\n<p>actions.move_to_element(video_element).perform()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u722c\u53d6\u7f51\u7ad9\u89c6\u9891\u65f6\uff0c<strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3001\u89e3\u6790HTML\u5185\u5bb9\u3001\u4f7f\u7528youtube-dl\u4e0b\u8f7d\u89c6\u9891\u3001\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\u548c\u53cd\u722c\u866b\u673a\u5236<\/strong>\u662f\u5173\u952e\u6b65\u9aa4\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u6210\u529f\u5730\u722c\u53d6\u7f51\u7ad9\u89c6\u9891\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u8fdb\u884c\u722c\u866b\u64cd\u4f5c\u65f6\uff0c\u4e00\u5b9a\u8981\u9075\u5b88\u7f51\u7ad9\u7684<code>robots.txt<\/code>\u6587\u4ef6\u548c\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import youtube_dl<\/p>\n<p>from selenium import webdriver<\/p>\n<p>from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.chrome.service import Service<\/p>\n<p>from webdriver_manager.chrome import ChromeDriverManager<\/p>\n<h2><strong>Step 1: Use requests to get the webpage content<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com\/video-page&#39;<\/p>\n<p>headers = {<\/p>\n<p>    &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers)<\/p>\n<p>html_content = response.content<\/p>\n<h2><strong>Step 2: Use BeautifulSoup to parse the HTML content<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p>video_tag = soup.find(&#39;video&#39;)<\/p>\n<p>video_url = video_tag[&#39;src&#39;] if video_tag else None<\/p>\n<h2><strong>Step 3: If video_url is None, use selenium to handle dynamic content<\/strong><\/h2>\n<p>if not video_url:<\/p>\n<p>    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))<\/p>\n<p>    driver.get(url)<\/p>\n<p>    video_element = driver.find_element(By.TAG_NAME, &#39;video&#39;)<\/p>\n<p>    video_url = video_element.get_attribute(&#39;src&#39;)<\/p>\n<p>    driver.quit()<\/p>\n<h2><strong>Step 4: Use youtube-dl to download the video<\/strong><\/h2>\n<p>if video_url:<\/p>\n<p>    ydl_opts = {}<\/p>\n<p>    with youtube_dl.YoutubeDL(ydl_opts) as ydl:<\/p>\n<p>        ydl.download([video_url])<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to find video URL.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5168\u9762\u5730\u722c\u53d6\u7f51\u7ad9\u89c6\u9891\uff0c\u5e76\u5904\u7406\u5404\u79cd\u6f5c\u5728\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u722c\u53d6\u7279\u5b9a\u7f51\u7ad9\u7684\u89c6\u9891\uff1f<\/strong><br \/>\u8981\u722c\u53d6\u7279\u5b9a\u7f51\u7ad9\u7684\u89c6\u9891\uff0c\u9996\u5148\u9700\u8981\u5206\u6790\u8be5\u7f51\u7ad9\u7684\u7ed3\u6784\uff0c\u786e\u5b9a\u89c6\u9891\u7684\u6e90URL\u3002\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5e93\uff0c\u5982requests\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0cBeautifulSoup\u89e3\u6790HTML\u3002\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u7684\u89c6\u9891\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\u3002\u786e\u4fdd\u9075\u5faa\u7f51\u7ad9\u7684\u722c\u866b\u534f\u8bae\u548c\u6cd5\u5f8b\u89c4\u5b9a\u3002<\/p>\n<p><strong>\u722c\u53d6\u89c6\u9891\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u548c\u9053\u5fb7\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u722c\u53d6\u89c6\u9891\u5185\u5bb9\u65f6\uff0c\u5fc5\u987b\u9075\u5faa\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u5305\u62ec\u7248\u6743\u6cd5\u548c\u7f51\u7ad9\u7684\u670d\u52a1\u6761\u6b3e\u3002\u786e\u4fdd\u60a8\u83b7\u5f97\u9002\u5f53\u7684\u6388\u6743\u6216\u8bb8\u53ef\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u53d7\u7248\u6743\u4fdd\u62a4\u7684\u5185\u5bb9\u3002\u6b64\u5916\uff0c\u907f\u514d\u5bf9\u7f51\u7ad9\u9020\u6210\u8fc7\u591a\u7684\u8bf7\u6c42\u8d1f\u62c5\uff0c\u4ee5\u514d\u5f71\u54cd\u5176\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p><strong>\u54ea\u4e9bPython\u5e93\u9002\u5408\u7528\u4e8e\u89c6\u9891\u722c\u53d6\uff1f<\/strong><br \/>\u5e38\u7528\u7684Python\u5e93\u5305\u62ecrequests\uff08\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff09\u3001BeautifulSoup\uff08\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\uff09\u3001Selenium\uff08\u7528\u4e8e\u5904\u7406\u52a8\u6001\u7f51\u9875\uff09\u548cPyTube\uff08\u4e13\u95e8\u7528\u4e8e\u4e0b\u8f7dYouTube\u89c6\u9891\uff09\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u80fd\u6709\u6548\u63d0\u53d6\u89c6\u9891\u94fe\u63a5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u5c06\u63d0\u9ad8\u722c\u53d6\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u722c\u53d6\u7f51\u7ad9\u89c6\u9891\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u7f51\u7edc\u8bf7\u6c42\u5e93\u3001\u89e3\u6790HTML\u5185\u5bb9\u7684\u5e93\u548c\u89c6\u9891\u4e0b\u8f7d\u5de5\u5177\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528r [&hellip;]","protected":false},"author":3,"featured_media":1159932,"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\/1159926"}],"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=1159926"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159926\/revisions"}],"predecessor-version":[{"id":1159933,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159926\/revisions\/1159933"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159932"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}