{"id":1074858,"date":"2025-01-08T11:38:31","date_gmt":"2025-01-08T03:38:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074858.html"},"modified":"2025-01-08T11:38:34","modified_gmt":"2025-01-08T03:38:34","slug":"python%e6%9c%89%e4%b8%a4%e4%b8%aa%e6%a0%87%e7%ad%be%e5%a6%82%e4%bd%95%e5%ae%9a%e4%bd%8d-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1074858.html","title":{"rendered":"python\u6709\u4e24\u4e2a\u6807\u7b7e\u5982\u4f55\u5b9a\u4f4d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104404\/439e3365-3007-486d-95ec-22a1011fe371.webp\" alt=\"python\u6709\u4e24\u4e2a\u6807\u7b7e\u5982\u4f55\u5b9a\u4f4d\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7\u4f7f\u7528Python\u7684Selenium\u5e93\u3001BeautifulSoup\u5e93\u3001XPath\u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9HTML\u9875\u9762\u4e2d\u4e24\u4e2a\u6807\u7b7e\u7684\u5b9a\u4f4d<\/strong>\u3002\u5176\u4e2d\uff0cSelenium\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u6765\u63a7\u5236\u548c\u64cd\u4f5c\u6d4f\u89c8\u5668\uff0cBeautifulSoup\u5e93\u5219\u9002\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\uff0cXPath\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u7cbe\u786e\u5b9a\u4f4d\u6807\u7b7e\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528Selenium\u5e93\u548cXPath\u8868\u8fbe\u5f0f\u6765\u5b9a\u4f4d\u4e24\u4e2a\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Selenium\u5e93\u5b9a\u4f4d\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316Web\u6d4f\u89c8\u5668\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u6d4b\u8bd5Web\u5e94\u7528\u7a0b\u5e8f\u3002\u901a\u8fc7Selenium\uff0c\u6211\u4eec\u53ef\u4ee5\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u52a0\u8f7d\u9875\u9762\uff0c\u7136\u540e\u5b9a\u4f4d\u548c\u64cd\u4f5c\u9875\u9762\u4e0a\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Selenium<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Selenium\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd8\u9700\u8981\u4e0b\u8f7d\u4e0e\u6d4f\u89c8\u5668\u5339\u914d\u7684WebDriver\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8eChrome\u6d4f\u89c8\u5668\uff0c\u53ef\u4ee5\u4e0b\u8f7d<a href=\"http:\/\/chromedriver.chromium.org\/downloads\">ChromeDriver<\/a>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u521d\u59cb\u5316WebDriver<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u521d\u59cb\u5316WebDriver\u5e76\u52a0\u8f7d\u7f51\u9875\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u8bbe\u7f6eWebDriver\u8def\u5f84<\/strong><\/h2>\n<p>driver_path = &#39;path_to_webdriver&#39;<\/p>\n<p>driver = webdriver.Chrome(executable_path=driver_path)<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528XPath\u5b9a\u4f4d\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>XPath\u662f\u4e00\u79cd\u5728XML\u6587\u6863\u4e2d\u67e5\u627e\u4fe1\u606f\u7684\u8bed\u8a00\u3002HTML\u6587\u6863\u5176\u5b9e\u662fXML\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u6240\u4ee5XPath\u4e5f\u53ef\u4ee5\u7528\u6765\u5b9a\u4f4dHTML\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0bHTML\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;html&gt;<\/p>\n<p>  &lt;body&gt;<\/p>\n<p>    &lt;div id=&quot;content&quot;&gt;<\/p>\n<p>      &lt;p class=&quot;text&quot;&gt;First paragraph&lt;\/p&gt;<\/p>\n<p>      &lt;p class=&quot;text&quot;&gt;Second paragraph&lt;\/p&gt;<\/p>\n<p>    &lt;\/div&gt;<\/p>\n<p>  &lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8981\u5b9a\u4f4d\u8fd9\u4e24\u4e2a<code>&lt;p&gt;<\/code>\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bXPath\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4f4d\u7b2c\u4e00\u4e2a&lt;p&gt;\u6807\u7b7e<\/p>\n<p>first_paragraph = driver.find_element_by_xpath(&#39;\/\/div[@id=&quot;content&quot;]\/p[1]&#39;)<\/p>\n<p>print(first_paragraph.text)  # \u8f93\u51fa\uff1aFirst paragraph<\/p>\n<h2><strong>\u5b9a\u4f4d\u7b2c\u4e8c\u4e2a&lt;p&gt;\u6807\u7b7e<\/strong><\/h2>\n<p>second_paragraph = driver.find_element_by_xpath(&#39;\/\/div[@id=&quot;content&quot;]\/p[2]&#39;)<\/p>\n<p>print(second_paragraph.text)  # \u8f93\u51fa\uff1aSecond paragraph<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528BeautifulSoup\u5e93\u5b9a\u4f4d\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684API\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u6587\u6863\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5BeautifulSoup<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5BeautifulSoup\u5e93\u548crequests\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528BeautifulSoup\u8fdb\u884c\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>html_content = response.content<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b9a\u4f4d\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u63d0\u4f9b\u7684\u5404\u79cd\u65b9\u6cd5\u6765\u5b9a\u4f4d\u6807\u7b7e\uff0c\u4f8b\u5982<code>find<\/code>\u548c<code>find_all<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4f4d\u6240\u6709&lt;p&gt;\u6807\u7b7e<\/p>\n<p>paragraphs = soup.find_all(&#39;p&#39;, class_=&#39;text&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u7b2c\u4e00\u4e2a&lt;p&gt;\u6807\u7b7e\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>print(paragraphs[0].text)  # \u8f93\u51fa\uff1aFirst paragraph<\/p>\n<h2><strong>\u8f93\u51fa\u7b2c\u4e8c\u4e2a&lt;p&gt;\u6807\u7b7e\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>print(paragraphs[1].text)  # \u8f93\u51fa\uff1aSecond paragraph<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u4f7f\u7528Selenium\u548cBeautifulSoup<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u5355\u72ec\u4f7f\u7528Selenium\u6216\u8005BeautifulSoup\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u3002\u53ef\u4ee5\u7ed3\u5408\u4e24\u8005\u7684\u4f18\u70b9\uff0c\u4f7f\u7528Selenium\u52a0\u8f7d\u9875\u9762\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u89e3\u6790\u9875\u9762\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u52a0\u8f7d\u9875\u9762\u5e76\u83b7\u53d6HTML\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u8bbe\u7f6eWebDriver\u8def\u5f84<\/strong><\/h2>\n<p>driver_path = &#39;path_to_webdriver&#39;<\/p>\n<p>driver = webdriver.Chrome(executable_path=driver_path)<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u9875\u9762HTML\u5185\u5bb9<\/strong><\/h2>\n<p>html_content = driver.page_source<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u89e3\u6790HTML\u5185\u5bb9<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u5b9a\u4f4d\u6240\u6709&lt;p&gt;\u6807\u7b7e<\/strong><\/h2>\n<p>paragraphs = soup.find_all(&#39;p&#39;, class_=&#39;text&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u7b2c\u4e00\u4e2a&lt;p&gt;\u6807\u7b7e\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>print(paragraphs[0].text)  # \u8f93\u51fa\uff1aFirst paragraph<\/p>\n<h2><strong>\u8f93\u51fa\u7b2c\u4e8c\u4e2a&lt;p&gt;\u6807\u7b7e\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>print(paragraphs[1].text)  # \u8f93\u51fa\uff1aSecond paragraph<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b9a\u4f4dHTML\u9875\u9762\u4e2d\u7684\u4e24\u4e2a\u6807\u7b7e\u3002Selenium\u9002\u5408\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u9875\u9762\uff0c\u800cBeautifulSoup\u9002\u5408\u5904\u7406\u9759\u6001\u9875\u9762\u3002\u7ed3\u5408\u4f7f\u7528Selenium\u548cBeautifulSoup\uff0c\u53ef\u4ee5\u5145\u5206\u5229\u7528\u4e24\u8005\u7684\u4f18\u52bf\uff0c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u7f51\u9875\u6570\u636e\u63d0\u53d6\u4efb\u52a1\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u8fdb\u884c\u7f51\u9875\u6807\u7b7e\u5b9a\u4f4d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u591a\u4e2a\u6807\u7b7e\u8fdb\u884c\u5b9a\u4f4d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528Beautiful Soup\u5e93\u6765\u89e3\u6790HTML\u6587\u6863\uff0c\u901a\u8fc7\u6807\u7b7e\u540d\u79f0\u3001\u5c5e\u6027\u503c\u7b49\u591a\u79cd\u65b9\u5f0f\u7cbe\u786e\u5b9a\u4f4d\u3002\u4f7f\u7528<code>find()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u6807\u7b7e\uff0c\u800c<code>find_all()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u6807\u7b7e\u3002\u7ed3\u5408\u6807\u7b7e\u540d\u548c\u5c5e\u6027\uff0c\u53ef\u4ee5\u7075\u6d3b\u8fdb\u884c\u5b9a\u4f4d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7<code>soup.find(&#39;div&#39;, {&#39;class&#39;: &#39;example&#39;})<\/code>\u6765\u67e5\u627e\u7c7b\u540d\u4e3a\u201cexample\u201d\u7684div\u6807\u7b7e\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Selenium\u65f6\uff0c\u5982\u4f55\u5904\u7406\u4e24\u4e2a\u6807\u7b7e\u7684\u5b9a\u4f4d\u95ee\u9898\uff1f<\/strong><br \/>\u4f7f\u7528Selenium\u8fdb\u884c\u7f51\u9875\u81ea\u52a8\u5316\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7XPath\u6216CSS\u9009\u62e9\u5668\u6765\u5b9a\u4f4d\u591a\u4e2a\u6807\u7b7e\u3002XPath\u5141\u8bb8\u4f7f\u7528\u903b\u8f91\u8fd0\u7b97\u7b26\uff0c\u80fd\u591f\u7075\u6d3b\u5730\u5b9a\u4f4d\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>\/\/div[@class=&#39;example&#39; or @id=&#39;unique&#39;]<\/code>\u6765\u5b9a\u4f4d\u7c7b\u540d\u4e3a\u201cexample\u201d\u6216ID\u4e3a\u201cunique\u201d\u7684div\u6807\u7b7e\u3002CSS\u9009\u62e9\u5668\u540c\u6837\u652f\u6301\u7ec4\u5408\u9009\u62e9\u5668\uff0c\u5982<code>div.example, div#unique<\/code>\u3002<\/p>\n<p><strong>\u5b9a\u4f4d\u591a\u4e2a\u6807\u7b7e\u65f6\uff0c\u6027\u80fd\u662f\u5426\u4f1a\u53d7\u5230\u5f71\u54cd\uff1f<\/strong><br \/>\u5728\u5904\u7406\u591a\u4e2a\u6807\u7b7e\u7684\u5b9a\u4f4d\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u4e00\u5b9a\u5f71\u54cd\u3002\u4f7f\u7528<code>find_all()<\/code>\u65b9\u6cd5\u6216\u590d\u6742\u7684XPath\u53ef\u80fd\u4f1a\u5bfc\u81f4\u89e3\u6790\u65f6\u95f4\u5ef6\u957f\uff0c\u5c24\u5176\u662f\u5728\u89e3\u6790\u5927\u578bHTML\u6587\u6863\u65f6\u3002\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u7f29\u5c0f\u641c\u7d22\u8303\u56f4\uff0c\u4f8b\u5982\u5148\u5b9a\u4f4d\u5230\u67d0\u4e2a\u7236\u5143\u7d20\uff0c\u518d\u5728\u5176\u5b50\u5143\u7d20\u4e2d\u67e5\u627e\u76ee\u6807\u6807\u7b7e\u3002\u6b64\u5916\uff0c\u4f18\u5316\u6807\u7b7e\u9009\u62e9\u548c\u5c5e\u6027\u8fc7\u6ee4\u4e5f\u80fd\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u67e5\u627e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7\u4f7f\u7528Python\u7684Selenium\u5e93\u3001BeautifulSoup\u5e93\u3001XPath\u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9HT [&hellip;]","protected":false},"author":3,"featured_media":1074868,"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\/1074858"}],"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=1074858"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074858\/revisions"}],"predecessor-version":[{"id":1074870,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074858\/revisions\/1074870"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1074868"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1074858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1074858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1074858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}