{"id":1101862,"date":"2025-01-08T15:54:02","date_gmt":"2025-01-08T07:54:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101862.html"},"modified":"2025-01-08T15:54:06","modified_gmt":"2025-01-08T07:54:06","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%bd%8dh5%e9%a1%b5%e9%9d%a2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1101862.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4f4dh5\u9875\u9762"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064438\/af1c6223-dcbd-42c5-b2e4-bba0143338bd.webp\" alt=\"python\u5982\u4f55\u5b9a\u4f4dh5\u9875\u9762\" \/><\/p>\n<p><p> <strong>Python \u5b9a\u4f4d H5 \u9875\u9762\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 Selenium\u3001\u5229\u7528 BeautifulSoup\u3001\u7ed3\u5408 Requests \u6a21\u5757\u3001\u8fd0\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528 Selenium \u662f\u6700\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002Selenium \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u5316 web \u6d4f\u89c8\u5668\uff0c\u4ee5\u4fbf\u5728\u6d4f\u89c8\u5668\u4e2d\u5b9a\u4f4d\u548c\u64cd\u4f5c HTML5 \u9875\u9762\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 Selenium<\/h3>\n<\/p>\n<p><p>Selenium \u662f\u4e00\u4e2a\u7528\u4e8e web \u5e94\u7528\u7a0b\u5e8f\u6d4b\u8bd5\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7 WebDriver \u6a21\u62df\u6d4f\u89c8\u5668\u7684\u64cd\u4f5c\uff0c\u5b9a\u4f4d\u548c\u64cd\u4f5c HTML5 \u9875\u9762\u5143\u7d20\u3002Selenium \u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5305\u62ec Python\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 Selenium \u5b9a\u4f4d H5 \u9875\u9762\u5143\u7d20\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5 Selenium \u548c WebDriver<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5 Selenium \u5305\u548c\u76f8\u5e94\u7684 WebDriver\u3002\u4ee5 Chrome \u6d4f\u89c8\u5668\u4e3a\u4f8b\uff0c\u9700\u8981\u5b89\u88c5 ChromeDriver\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4e0b\u8f7d\u4e0e Chrome \u6d4f\u89c8\u5668\u7248\u672c\u5339\u914d\u7684 ChromeDriver\uff0c\u5e76\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u5bfc\u5165 Selenium \u5e76\u542f\u52a8\u6d4f\u89c8\u5668<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 Selenium \u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u8bbf\u95ee\u76ee\u6807 H5 \u9875\u9762\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.support.ui import WebDriverW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/p>\n<p>from selenium.webdriver.support import expected_conditions as EC<\/p>\n<h2><strong>\u542f\u52a8 Chrome \u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u8bbf\u95ee\u76ee\u6807 H5 \u9875\u9762<\/strong><\/h2>\n<p>driver.get(&#39;https:\/\/example.com&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b9a\u4f4d\u548c\u64cd\u4f5c HTML5 \u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 Selenium \u63d0\u4f9b\u7684\u5404\u79cd\u5b9a\u4f4d\u7b56\u7565\u6765\u67e5\u627e\u5e76\u64cd\u4f5c HTML5 \u9875\u9762\u5143\u7d20\u3002\u5e38\u7528\u7684\u5b9a\u4f4d\u7b56\u7565\u5305\u62ec ID\u3001XPath\u3001CSS \u9009\u62e9\u5668\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u901a\u8fc7 ID \u5b9a\u4f4d\u5143\u7d20<\/p>\n<p>element = driver.find_element(By.ID, &#39;element_id&#39;)<\/p>\n<h2><strong>\u901a\u8fc7 XPath \u5b9a\u4f4d\u5143\u7d20<\/strong><\/h2>\n<p>element = driver.find_element(By.XPATH, &#39;\/\/*[@id=&quot;element_id&quot;]&#39;)<\/p>\n<h2><strong>\u901a\u8fc7 CSS \u9009\u62e9\u5668\u5b9a\u4f4d\u5143\u7d20<\/strong><\/h2>\n<p>element = driver.find_element(By.CSS_SELECTOR, &#39;#element_id&#39;)<\/p>\n<h2><strong>\u64cd\u4f5c\u5143\u7d20\uff0c\u4f8b\u5982\u70b9\u51fb\u3001\u8f93\u5165\u6587\u672c\u7b49<\/strong><\/h2>\n<p>element.click()<\/p>\n<p>element.send_keys(&#39;text&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u7b49\u5f85\u9875\u9762\u4e0a\u7684\u67d0\u4e9b\u5143\u7d20\u52a0\u8f7d\u5b8c\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528 WebDriverWait\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b49\u5f85\u9875\u9762\u4e0a\u7684\u67d0\u4e2a\u5143\u7d20\u52a0\u8f7d\u5b8c\u6210<\/p>\n<p>element = WebDriverWait(driver, 10).until(<\/p>\n<p>    EC.presence_of_element_located((By.ID, &#39;element_id&#39;))<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5173\u95ed\u6d4f\u89c8\u5668<\/h4>\n<\/p>\n<p><p>\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u8bb0\u5f97\u5173\u95ed\u6d4f\u89c8\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528 BeautifulSoup<\/h3>\n<\/p>\n<p><p>BeautifulSoup \u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790 HTML \u548c XML \u6587\u6863\u7684\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u5b83\u901a\u5e38\u4e0e Requests \u6a21\u5757\u7ed3\u5408\u4f7f\u7528\u3002\u867d\u7136 BeautifulSoup \u65e0\u6cd5\u50cf Selenium \u90a3\u6837\u64cd\u4f5c\u6d4f\u89c8\u5668\uff0c\u4f46\u5b83\u975e\u5e38\u9002\u5408\u7528\u4e8e\u9759\u6001\u9875\u9762\u7684\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5 BeautifulSoup \u548c Requests<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u5e76\u89e3\u6790<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u53d1\u9001 HTTP \u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u5b9a\u4f4d HTML5 \u5143\u7d20<\/strong><\/h2>\n<p>element = soup.find(id=&#39;element_id&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408 Requests \u6a21\u5757<\/h3>\n<\/p>\n<p><p>Requests \u6a21\u5757\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684 HTTP \u8bf7\u6c42\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u7f51\u9875\u7684 HTML \u5185\u5bb9\u3002\u7ed3\u5408 BeautifulSoup\uff0c\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790\u548c\u63d0\u53d6 H5 \u9875\u9762\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u53d1\u9001 HTTP \u8bf7\u6c42<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001 HTTP GET \u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>html_content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u6790 HTML \u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u7ed3\u5408 BeautifulSoup \u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u89e3\u6790 HTML \u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u5b9a\u4f4d HTML5 \u5143\u7d20<\/strong><\/h2>\n<p>element = soup.find(id=&#39;element_id&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8fd0\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u548c\u63d0\u53d6 HTML5 \u9875\u9762\u4e2d\u7684\u7279\u5b9a\u5185\u5bb9\u3002\u867d\u7136\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0d\u5982 BeautifulSoup \u76f4\u89c2\uff0c\u4f46\u5b83\u5728\u5904\u7406\u4e00\u4e9b\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u5339\u914d\u4efb\u52a1\u65f6\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>\u5bfc\u5165 re \u6a21\u5757\u5e76\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/strong><\/h2>\n<p>pattern = re.compile(r&#39;&lt;div id=&quot;element_id&quot;&gt;(.*?)&lt;\/div&gt;&#39;)<\/p>\n<h2><strong>\u5339\u914d\u5e76\u63d0\u53d6\u5185\u5bb9<\/strong><\/h2>\n<p>matches = pattern.findall(html_content)<\/p>\n<p>for match in matches:<\/p>\n<p>    print(match)<\/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\u53ef\u4ee5\u4f7f\u7528 Python \u6709\u6548\u5730\u5b9a\u4f4d\u548c\u64cd\u4f5c H5 \u9875\u9762\u3002<strong>Selenium<\/strong> \u662f\u6700\u5e38\u89c1\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4e0e\u9875\u9762\u4ea4\u4e92\u7684\u573a\u666f\uff1b<strong>BeautifulSoup<\/strong> \u5219\u9002\u5408\u7528\u4e8e\u9759\u6001\u9875\u9762\u7684\u6570\u636e\u63d0\u53d6\uff1b\u7ed3\u5408 <strong>Requests<\/strong> \u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff1b<strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong> \u5219\u9002\u7528\u4e8e\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u5339\u914d\u4efb\u52a1\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210 H5 \u9875\u9762\u5b9a\u4f4d\u548c\u64cd\u4f5c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u81ea\u52a8\u5316\u5b9a\u4f4dH5\u9875\u9762\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5728H5\u9875\u9762\u4e2d\u5b9a\u4f4d\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684Selenium\u5e93\u3002Selenium\u5141\u8bb8\u60a8\u6a21\u62df\u7528\u6237\u4e0e\u7f51\u9875\u7684\u4ea4\u4e92\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7XPath\u3001CSS\u9009\u62e9\u5668\u6216\u5143\u7d20\u7684ID\u8fdb\u884c\u5b9a\u4f4d\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Selenium\u5e93\uff0c\u5e76\u4e0b\u8f7d\u76f8\u5e94\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\u3002\u4f7f\u7528<code>driver.find_element_by_xpath()<\/code>\u6216<code>driver.find_element_by_css_selector()<\/code>\u7b49\u65b9\u6cd5\u6765\u5b9a\u4f4d\u9875\u9762\u5143\u7d20\u3002\u786e\u4fdd\u5728\u64cd\u4f5c\u524d\u4f7f\u7528<code>driver.get()<\/code>\u65b9\u6cd5\u52a0\u8f7d\u76ee\u6807\u9875\u9762\u3002<\/p>\n<p><strong>\u5728H5\u9875\u9762\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8bb8\u591aH5\u9875\u9762\u4f7f\u7528AJAX\u6280\u672f\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5143\u7d20\u5728\u9875\u9762\u52a0\u8f7d\u65f6\u4e0d\u53ef\u89c1\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u4e2d\u7684WebDriverWait\u7c7b\uff0c\u4ee5\u7b49\u5f85\u7279\u5b9a\u5143\u7d20\u7684\u51fa\u73b0\u3002\u901a\u8fc7\u8bbe\u7f6e\u9002\u5f53\u7684\u7b49\u5f85\u6761\u4ef6\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u5728\u5c1d\u8bd5\u5b9a\u4f4d\u5143\u7d20\u4e4b\u524d\uff0c\u5b83\u4eec\u5df2\u7ecf\u5b8c\u5168\u52a0\u8f7d\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u5143\u7d20\u4e0d\u5b58\u5728\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728H5\u9875\u9762\u4e0a\u8fdb\u884c\u8868\u5355\u81ea\u52a8\u586b\u5199\u548c\u63d0\u4ea4\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u5e2e\u52a9\u60a8\u81ea\u52a8\u586b\u5199H5\u9875\u9762\u4e0a\u7684\u8868\u5355\u3002\u4f7f\u7528Selenium\u5b9a\u4f4d\u8f93\u5165\u6846\u5143\u7d20\u5e76\u4f7f\u7528<code>send_keys()<\/code>\u65b9\u6cd5\u8f93\u5165\u76f8\u5e94\u7684\u6570\u636e\u3002\u586b\u5199\u5b8c\u6210\u540e\uff0c\u901a\u8fc7\u5b9a\u4f4d\u63d0\u4ea4\u6309\u94ae\u5e76\u8c03\u7528<code>.click()<\/code>\u65b9\u6cd5\u6765\u63d0\u4ea4\u8868\u5355\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u8868\u5355\u63d0\u4ea4\u540e\u7684\u54cd\u5e94\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u7684<code>driver.current_url<\/code>\u6216<code>driver.page_source<\/code>\u6765\u83b7\u53d6\u9875\u9762\u5185\u5bb9\uff0c\u4ee5\u9a8c\u8bc1\u63d0\u4ea4\u662f\u5426\u6210\u529f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5b9a\u4f4d H5 \u9875\u9762\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 Selenium\u3001\u5229\u7528 BeautifulSoup\u3001\u7ed3\u5408 Requ [&hellip;]","protected":false},"author":3,"featured_media":1101874,"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\/1101862"}],"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=1101862"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101862\/revisions"}],"predecessor-version":[{"id":1101877,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101862\/revisions\/1101877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101874"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}