{"id":179665,"date":"2024-05-08T20:18:59","date_gmt":"2024-05-08T12:18:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/179665.html"},"modified":"2024-05-08T20:19:09","modified_gmt":"2024-05-08T12:19:09","slug":"python%e8%87%aa%e5%8a%a8%e5%8c%96%e7%bd%91%e9%a1%b5%e8%a1%a8%e5%8d%95%e5%a1%ab%e5%86%99%e7%94%a8%e5%93%aa%e4%b8%aa%e5%ba%93%e5%a5%bd","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/179665.html","title":{"rendered":"Python\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\u7528\u54ea\u4e2a\u5e93\u597d"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27064552\/ea249b26-413d-471d-926c-ef8a9c5ddcf9.webp\" alt=\"Python\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\u7528\u54ea\u4e2a\u5e93\u597d\" \/><\/p>\n<p><p>\u5bf9\u4e8e\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\uff0c<strong>Selenium \u548c Beautiful Soup\u662f\u6700\u4f73\u9009\u62e9<\/strong>\u3002Selenium \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u4e13\u4e3a\u7f51\u9875\u4ea4\u4e92\u8bbe\u8ba1\uff0c\u80fd\u591f\u6a21\u62df\u7528\u6237\u884c\u4e3a\u6765\u586b\u5199\u8868\u5355\u3001\u70b9\u51fb\u6309\u94ae\u7b49\u64cd\u4f5c\uff0c\u800c Beautiful Soup \u5219\u64c5\u957f\u4e8e\u89e3\u6790 HTML \u4ee3\u7801\uff0c\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u6240\u9700\u4fe1\u606f\uff0c<strong>\u4e24\u8005\u7ed3\u5408\u4f7f\u7528<\/strong>\uff0c\u53ef\u4ee5\u8fbe\u5230\u975e\u5e38\u597d\u7684\u81ea\u52a8\u5316\u8868\u5355\u586b\u5199\u6548\u679c\u3002\u5c24\u5176\u662f Selenium\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u5957\u5b8c\u6574\u7684API\u6765\u6a21\u62df\u7528\u6237\u64cd\u4f5c\u7f51\u9875\uff0c\u5982\u8f93\u5165\u6587\u672c\u3001\u70b9\u51fb\u6309\u94ae\u7b49\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u7f51\u9875\u8868\u5355\u81ea\u52a8\u586b\u5199\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Selenium\u7684\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>Selenium WebDriver\u662fSelenium\u7684\u6838\u5fc3\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u7f16\u5199\u4ee3\u7801\u4ee5\u6a21\u62df\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u5404\u79cd\u884c\u4e3a\uff0c\u5982\u70b9\u51fb\u3001\u586b\u5199\u8868\u5355\u7b49\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Selenium\u5305\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u5b8c\u6210\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u7740\uff0c\u9700\u8981\u4e0b\u8f7d\u76f8\u5e94\u6d4f\u89c8\u5668\u7684WebDriver\uff0c\u4f8b\u5982Chrome\u6d4f\u89c8\u5668\u7684ChromeDriver\uff0c\u5e76\u786e\u4fdd\u5176\u8def\u5f84\u5df2\u7ecf\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u6216\u5728\u4ee3\u7801\u4e2d\u76f4\u63a5\u6307\u5b9a\u5176\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Selenium\u4f7f\u7528\u4f8b\u5b50\uff0c\u8be5\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u6253\u5f00Google\u9996\u9875\u5e76\u5728\u641c\u7d22\u6846\u4e2d\u8f93\u5165\u5173\u952e\u8bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>driver = webdriver.Chrome(&#039;\/path\/to\/chromedriver&#039;) # \u6307\u5b9aChromeDriver\u8def\u5f84<\/p>\n<p>driver.get(&quot;http:\/\/www.google.com&quot;) # \u6253\u5f00Google\u9996\u9875<\/p>\n<p>search_box = driver.find_element_by_name(&#039;q&#039;) # \u627e\u5230\u641c\u7d22\u6846<\/p>\n<p>search_box.send_keys(&#039;Python&#039;) # \u8f93\u5165\u641c\u7d22\u5173\u952e\u8bcd<\/p>\n<p>search_box.submit() # \u63d0\u4ea4\u641c\u7d22\u8bf7\u6c42<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7Selenium\u6a21\u62df\u7528\u6237\u884c\u4e3a\u586b\u5199\u7f51\u9875\u8868\u5355\u65f6\uff0c\u5173\u952e\u5728\u4e8e\u51c6\u786e\u5730\u5b9a\u4f4d\u5230\u8868\u5355\u5143\u7d20\uff0c\u5e76\u901a\u8fc7<code>send_keys()<\/code>\u65b9\u6cd5\u8f93\u5165\u5185\u5bb9\uff0c\u6700\u540e\u901a\u8fc7\u70b9\u51fb\u63d0\u4ea4\u6309\u94ae\u6216\u76f4\u63a5\u8c03\u7528\u8868\u5355\u7684<code>submit()<\/code>\u65b9\u6cd5\u5b8c\u6210\u8868\u5355\u63d0\u4ea4\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001Beautiful Soup\u7684\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>Beautiful Soup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684Python\u5e93\uff0c\u5b83\u521b\u5efa\u4e86\u4e00\u4e2a\u89e3\u6790\u6811\uff0c\u4f7f\u5f00\u53d1\u8005\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6570\u636e\u3002\u5728\u8fdb\u884c\u7f51\u9875\u8868\u5355\u81ea\u52a8\u5316\u586b\u5199\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u83b7\u53d6\u4e00\u4e9b\u6570\u636e\u6765\u586b\u5145\u8868\u5355\uff0cBeautiful Soup\u5728\u8fd9\u4e00\u6b65\u9aa4\u4e2d\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5Beautiful Soup\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u901a\u8fc7pip\u5b89\u88c5\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528Beautiful Soup\u89e3\u6790\u7b80\u5355HTML\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_doc = &quot;&quot;&quot;<\/p>\n<p>&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse&#039;s story&lt;\/title&gt;&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse&#039;s story&lt;\/b&gt;&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;&lt;\/html&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>soup = BeautifulSoup(html_doc, &#039;html.parser&#039;) # \u89e3\u6790HTML\u6587\u6863<\/p>\n<p>print(soup.title) # \u6253\u5370&lt;title&gt;\u6807\u7b7e\u7684\u5185\u5bb9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aBeautifulSoup\u5bf9\u8c61\uff0c\u5c06\u4e00\u6bb5HTML\u4ee3\u7801\u4f5c\u4e3a\u8f93\u5165\uff0c\u4e4b\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u63d0\u4f9b\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u5c5e\u6027\u6765\u63d0\u53d6\u6216\u64cd\u4f5cHTML\u4e2d\u7684\u4e0d\u540c\u90e8\u5206\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u4f7f\u7528Selenium\u548cBeautiful Soup<\/h3>\n<\/p>\n<p><p>\u5728\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\u7684\u573a\u666f\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5148\u4ece\u67d0\u4e2a\u7f51\u9875\u4e0a\u6293\u53d6\u6570\u636e\uff0c\u7136\u540e\u518d\u586b\u5165\u53e6\u4e00\u4e2a\u8868\u5355\u4e2d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528Beautiful Soup\u6765\u89e3\u6790\u548c\u63d0\u53d6\u6570\u636e\uff0c\u7136\u540e\u518d\u7528Selenium\u6765\u586b\u5199\u8868\u5355\u548c\u63d0\u4ea4\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u4ece\u4e00\u4e2a\u7f51\u9875\u4e2d\u63d0\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u7136\u540e\u81ea\u52a8\u586b\u5199\u5230\u53e6\u4e00\u4e2a\u7f51\u9875\u7684\u6ce8\u518c\u8868\u5355\u4e2d\uff0c\u53ef\u4ee5\u5148\u7528Beautiful Soup\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u7136\u540e\u7528Selenium\u7684<code>send_keys()<\/code>\u65b9\u6cd5\u5c06\u8fd9\u4e9b\u4fe1\u606f\u586b\u5165\u8868\u5355\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9ad8\u7ea7\u6280\u5de7\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u867d\u7136Selenium\u5f88\u5f3a\u5927\uff0c\u4f46\u5b83\u4e5f\u6709\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u901a\u8fc7JavaScript\u52a8\u6001\u751f\u6210\u7684\u5185\u5bb9\uff0cSelenium\u53ef\u80fd\u9700\u8981\u7b49\u5f85JavaScript\u52a0\u8f7d\u5b8c\u6210\u624d\u80fd\u6b63\u786e\u64cd\u4f5c\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u7684\u7b49\u5f85\u6761\u4ef6\uff08\u5982<code>WebDriverW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/code> \u548c <code>expected_conditions<\/code>\u6a21\u5757\uff09\u6765\u786e\u4fdd\u9875\u9762\u5143\u7d20\u5df2\u7ecf\u52a0\u8f7d\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><p>\u53e6\u5916\uff0c\u9891\u7e41\u5730\u4f7f\u7528Selenium\u81ea\u52a8\u5316\u6a21\u62df\u7528\u6237\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u7f51\u7ad9\u91c7\u53d6\u53cd\u722c\u866b\u63aa\u65bd\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528Selenium\u8fdb\u884c\u5927\u89c4\u6a21\u7684\u6570\u636e\u91c7\u96c6\u6216\u8868\u5355\u586b\u5199\u65f6\uff0c\u5e94\u5f53\u6ce8\u610f\u6a21\u62df\u6b63\u5e38\u7528\u6237\u7684\u884c\u4e3a\uff0c\u907f\u514d\u8fc7\u5feb\u9891\u7e41\u5730\u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\uff0c<strong>\u7ed3\u5408\u4f7f\u7528Selenium\u548cBeautiful Soup<\/strong>\u4e0d\u4ec5\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u7f51\u9875\u8868\u5355\u7684\u81ea\u52a8\u5316\u586b\u5199\uff0c\u8fd8\u53ef\u4ee5\u5e94\u5bf9\u66f4\u591a\u590d\u6742\u7684Web\u81ea\u52a8\u5316\u4efb\u52a1\u3002\u53ea\u9700\u8bb0\u4f4f\uff0c\u6b63\u786e\u5730\u9009\u62e9\u5de5\u5177\u5e76\u638c\u63e1\u5b83\u4eec\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5c31\u80fd\u5728\u81ea\u52a8\u5316\u9886\u57df\u5927\u653e\u5f02\u5f69\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q\uff1a\u6709\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u7528\u4e8e\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\uff1f<\/strong><br \/>\nA\uff1aPython\u4e2d\u6709\u51e0\u4e2a\u6d41\u884c\u7684\u5e93\u53ef\u7528\u4e8e\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\uff0c\u5305\u62ecSelenium\u3001MechanicalSoup\u548cPyppeteer\u3002\u8fd9\u4e9b\u5e93\u90fd\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u6a21\u62df\u7528\u6237\u5728\u7f51\u9875\u4e0a\u586b\u5199\u8868\u5355\u5e76\u63d0\u4ea4\u6570\u636e\u3002<\/p>\n<p><strong>Q\uff1aSelenium\u9002\u5408\u7528\u4e8e\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\u5417\uff1f<\/strong><br \/>\nA\uff1a\u662f\u7684\uff0cSelenium\u662f\u4e00\u4e2a\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684Python\u5e93\uff0c\u53ef\u7528\u4e8e\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\u3002\u5b83\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u64cd\u4f5c\uff0c\u5305\u62ec\u586b\u5199\u8868\u5355\u3001\u70b9\u51fb\u6309\u94ae\u7b49\u3002Selenium\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u548c\u529f\u80fd\uff0c\u4ee5\u4fbf\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u7f51\u9875\u81ea\u52a8\u5316\u4efb\u52a1\u3002<\/p>\n<p><strong>Q\uff1aPyppeteer\u548cSelenium\u76f8\u6bd4\uff0c\u54ea\u4e2a\u66f4\u9002\u5408\u7528\u4e8e\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\uff1f<\/strong><br \/>\nA\uff1aPyppeteer\u662f\u4e00\u4e2a\u57fa\u4e8eChromium\u7684Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e0eSelenium\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u53ef\u7528\u4e8e\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\u3002\u4e0eSelenium\u76f8\u6bd4\uff0cPyppeteer\u66f4\u8f7b\u91cf\u7ea7\uff0c\u5e76\u4e14\u5728\u6027\u80fd\u65b9\u9762\u66f4\u6709\u4f18\u52bf\u3002\u6b64\u5916\uff0cPyppeteer\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u989d\u5916\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u622a\u56fe\u3001\u751f\u6210PDF\u7b49\u3002\u56e0\u6b64\uff0c\u5982\u679c\u53ea\u9700\u8981\u8fdb\u884c\u7b80\u5355\u7684\u7f51\u9875\u8868\u5355\u586b\u5199\uff0cPyppeteer\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5bf9\u4e8e\u81ea\u52a8\u5316\u7f51\u9875\u8868\u5355\u586b\u5199\uff0cSelenium \u548c Beautiful Soup\u662f\u6700\u4f73\u9009\u62e9\u3002Selenium \u662f\u4e00\u4e2a [&hellip;]","protected":false},"author":3,"featured_media":179679,"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\/179665"}],"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=179665"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179665\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/179679"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=179665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=179665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=179665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}