{"id":1023436,"date":"2024-12-30T14:05:10","date_gmt":"2024-12-30T06:05:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1023436.html"},"modified":"2024-12-30T14:05:12","modified_gmt":"2024-12-30T06:05:12","slug":"python%e5%a6%82%e4%bd%95%e8%87%aa%e5%8a%a8%e7%99%bb%e5%bd%95%e7%bd%91%e9%a1%b5-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1023436.html","title":{"rendered":"python\u5982\u4f55\u81ea\u52a8\u767b\u5f55\u7f51\u9875"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/9f56799b-5b52-4203-8c57-e6d4f9d984d7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u81ea\u52a8\u767b\u5f55\u7f51\u9875\" \/><\/p>\n<p><p> <strong>Python\u81ea\u52a8\u767b\u5f55\u7f51\u9875\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Selenium\u3001Requests\u5e93\u3001BeautifulSoup\u8fdb\u884c\u9875\u9762\u89e3\u6790\u3001\u6a21\u62df\u8868\u5355\u63d0\u4ea4\u7b49\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0cSelenium\u662f\u6700\u5e38\u7528\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u5b9e\u9645\u7684\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406JavaScript\u52a8\u6001\u5185\u5bb9\u7684\u7f51\u9875\u3002\u901a\u8fc7Selenium\uff0c\u6211\u4eec\u53ef\u4ee5\u6253\u5f00\u6d4f\u89c8\u5668\u3001\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u3001\u70b9\u51fb\u767b\u5f55\u6309\u94ae\u7b49\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u4f7f\u7528Selenium\u81ea\u52a8\u767b\u5f55\u7f51\u9875\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5b89\u88c5\u548c\u8bbe\u7f6eSelenium<\/strong><\/h2>\n<p><p>\u8981\u4f7f\u7528Selenium\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Selenium\u5e93\u548c\u6d4f\u89c8\u5668\u9a71\u52a8\u3002\u4ee5Chrome\u6d4f\u89c8\u5668\u4e3a\u4f8b\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5ChromeDriver\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\u9002\u5408\u4f60Chrome\u6d4f\u89c8\u5668\u7248\u672c\u7684ChromeDriver\uff0c\u5e76\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528Selenium\u6253\u5f00\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p><p>\u9996\u5148\u9700\u8981\u5bfc\u5165Selenium\u5e93\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u6d4f\u89c8\u5668\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u6253\u5f00Chrome\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u5bfc\u822a\u5230\u767b\u5f55\u9875\u9762<\/strong><\/h2>\n<p><p>\u4f7f\u7528<code>get<\/code>\u65b9\u6cd5\u5bfc\u822a\u5230\u76ee\u6807\u767b\u5f55\u9875\u9762\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">url = &quot;https:\/\/example.com\/login&quot;<\/p>\n<p>driver.get(url)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u627e\u5230\u5e76\u586b\u5199\u767b\u5f55\u8868\u5355<\/strong><\/h2>\n<p><p>\u4f7f\u7528Selenium\u7684<code>find_element_by_*<\/code>\u65b9\u6cd5\u627e\u5230\u767b\u5f55\u8868\u5355\u7684\u8f93\u5165\u6846\uff0c\u5e76\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">username = &quot;your_username&quot;<\/p>\n<p>password = &quot;your_password&quot;<\/p>\n<h2><strong>\u627e\u5230\u7528\u6237\u540d\u8f93\u5165\u6846\u5e76\u8f93\u5165\u7528\u6237\u540d<\/strong><\/h2>\n<p>username_field = driver.find_element_by_name(&quot;username&quot;)<\/p>\n<p>username_field.send_keys(username)<\/p>\n<h2><strong>\u627e\u5230\u5bc6\u7801\u8f93\u5165\u6846\u5e76\u8f93\u5165\u5bc6\u7801<\/strong><\/h2>\n<p>password_field = driver.find_element_by_name(&quot;password&quot;)<\/p>\n<p>password_field.send_keys(password)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u63d0\u4ea4\u8868\u5355<\/strong><\/h2>\n<p><p>\u627e\u5230\u767b\u5f55\u6309\u94ae\u5e76\u70b9\u51fb\uff0c\u63d0\u4ea4\u8868\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">login_button = driver.find_element_by_xpath(&quot;\/\/button[@type=&#39;submit&#39;]&quot;)<\/p>\n<p>login_button.click()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u5904\u7406\u767b\u5f55\u540e\u7684\u9875\u9762<\/strong><\/h2>\n<p><p>\u767b\u5f55\u6210\u529f\u540e\uff0c\u60a8\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528Selenium\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\uff0c\u4f8b\u5982\u6293\u53d6\u6570\u636e\u6216\u81ea\u52a8\u5316\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b49\u5f85\u9875\u9762\u52a0\u8f7d<\/p>\n<p>driver.implicitly_w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t(10)<\/p>\n<h2><strong>\u6293\u53d6\u767b\u5f55\u540e\u7684\u9875\u9762\u5185\u5bb9<\/strong><\/h2>\n<p>page_content = driver.page_source<\/p>\n<p>print(page_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p><p>\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u8bb0\u5f97\u5173\u95ed\u6d4f\u89c8\u5668\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4f7f\u7528Requests\u548cBeautifulSoup\u6a21\u62df\u767b\u5f55<\/strong><\/h2>\n<p><p>\u5982\u679c\u7f51\u9875\u4e0d\u5305\u542b\u590d\u6742\u7684JavaScript\u52a8\u6001\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528Requests\u5e93\u548cBeautifulSoup\u6765\u6a21\u62df\u767b\u5f55\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u8f7b\u91cf\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u7f51\u9875\u767b\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4f1a\u8bdd\u5bf9\u8c61<\/strong><\/h2>\n<p>session = requests.Session()<\/p>\n<h2><strong>\u83b7\u53d6\u767b\u5f55\u9875\u9762<\/strong><\/h2>\n<p>login_url = &quot;https:\/\/example.com\/login&quot;<\/p>\n<p>response = session.get(login_url)<\/p>\n<p>soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u627e\u5230\u5e76\u586b\u5199\u767b\u5f55\u8868\u5355<\/strong><\/h2>\n<p>login_data = {<\/p>\n<p>    &#39;username&#39;: &#39;your_username&#39;,<\/p>\n<p>    &#39;password&#39;: &#39;your_password&#39;,<\/p>\n<p>    &#39;csrf_token&#39;: soup.find(&#39;input&#39;, {&#39;name&#39;: &#39;csrf_token&#39;})[&#39;value&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u63d0\u4ea4\u767b\u5f55\u8868\u5355<\/strong><\/h2>\n<p>response = session.post(login_url, data=login_data)<\/p>\n<h2><strong>\u5904\u7406\u767b\u5f55\u540e\u7684\u9875\u9762<\/strong><\/h2>\n<p>dashboard_url = &quot;https:\/\/example.com\/dashboard&quot;<\/p>\n<p>response = session.get(dashboard_url)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u4f7f\u7528Python\u81ea\u52a8\u767b\u5f55\u7f51\u9875\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u9891\u7e41\u8bbf\u95ee\u548c\u64cd\u4f5c\u7f51\u9875\u7684\u573a\u666f\u4e0b\u3002Selenium\u9002\u7528\u4e8e\u5904\u7406\u590d\u6742\u7684JavaScript\u52a8\u6001\u7f51\u9875\uff0c\u800cRequests\u548cBeautifulSoup\u5219\u66f4\u9002\u5408\u7b80\u5355\u7684\u9759\u6001\u7f51\u9875\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u7ed3\u5408\u5b9e\u9645\u9700\u6c42\uff0c\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\u767b\u5f55\u548c\u6570\u636e\u6293\u53d6\u7684\u76ee\u6807\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u81ea\u52a8\u767b\u5f55\u7f51\u9875\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u8981\u5b9e\u73b0\u7f51\u9875\u7684\u81ea\u52a8\u767b\u5f55\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5e93\u5982Requests\u548cBeautifulSoup\u8fdb\u884cHTTP\u8bf7\u6c42\u548cHTML\u89e3\u6790\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u4e86\u89e3\u7f51\u9875\u7684\u767b\u5f55\u8868\u5355\u7ed3\u6784\uff0c\u5305\u62ec\u8868\u5355\u7684URL\u3001\u9700\u8981\u63d0\u4ea4\u7684\u5b57\u6bb5\u53ca\u5176\u540d\u79f0\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Requests\u5e93\u53d1\u9001POST\u8bf7\u6c42\uff0c\u4f20\u9012\u5fc5\u8981\u7684\u767b\u5f55\u51ed\u636e\u3002\u4e00\u65e6\u767b\u5f55\u6210\u529f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u540c\u4e00\u4e2a\u4f1a\u8bdd\u5bf9\u8c61\u8bbf\u95ee\u5176\u4ed6\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u9875\u9762\u3002<\/p>\n<p><strong>\u4f7f\u7528Selenium\u8fdb\u884c\u7f51\u9875\u81ea\u52a8\u767b\u5f55\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>Selenium\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u64cd\u4f5c\u3002\u4f7f\u7528Selenium\u8fdb\u884c\u81ea\u52a8\u767b\u5f55\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u80fd\u591f\u5904\u7406JavaScript\u6e32\u67d3\u7684\u52a8\u6001\u5185\u5bb9\u548c\u590d\u6742\u7684\u4ea4\u4e92\u3002\u5982\u679c\u76ee\u6807\u7f51\u7ad9\u4f7f\u7528\u4e86\u5f88\u591a\u524d\u7aef\u6846\u67b6\u6216\u9700\u8981\u6267\u884cJavaScript\u624d\u80fd\u5b8c\u6210\u767b\u5f55\uff0cSelenium\u5c06\u662f\u66f4\u5408\u9002\u7684\u9009\u62e9\u3002\u6b64\u5916\uff0cSelenium\u80fd\u591f\u5904\u7406\u9a8c\u8bc1\u7801\u7b49\u5b89\u5168\u63aa\u65bd\uff0c\u867d\u7136\u8fd9\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u767b\u5f55\u540e\u7684\u4f1a\u8bdd\u4fdd\u6301\uff1f<\/strong><br \/>\u5728\u81ea\u52a8\u767b\u5f55\u540e\uff0c\u4fdd\u6301\u4f1a\u8bdd\u662f\u786e\u4fdd\u540e\u7eed\u8bf7\u6c42\u80fd\u591f\u6210\u529f\u7684\u5173\u952e\u3002\u4f7f\u7528Requests\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7Session\u5bf9\u8c61\u6765\u4fdd\u6301\u4f1a\u8bdd\u72b6\u6001\uff0c\u6240\u6709\u7684\u8bf7\u6c42\u90fd\u5c06\u4fdd\u6301\u8be5\u4f1a\u8bdd\u7684cookies\u3002\u82e5\u4f7f\u7528Selenium\uff0c\u767b\u5f55\u540e\u7684\u6d4f\u89c8\u5668\u4f1a\u8bdd\u4f1a\u81ea\u52a8\u4fdd\u6301\uff0c\u60a8\u53ea\u9700\u5728\u540c\u4e00\u5b9e\u4f8b\u4e2d\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\u3002\u5728\u5904\u7406\u4f1a\u8bdd\u65f6\uff0c\u786e\u4fdd\u5bf9\u53ef\u80fd\u7684\u4f1a\u8bdd\u8fc7\u671f\u60c5\u51b5\u505a\u597d\u5904\u7406\uff0c\u4f8b\u5982\u91cd\u65b0\u767b\u5f55\u6216\u66f4\u65b0cookies\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u81ea\u52a8\u767b\u5f55\u7f51\u9875\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Selenium\u3001Requests\u5e93\u3001BeautifulSoup\u8fdb\u884c\u9875\u9762 [&hellip;]","protected":false},"author":3,"featured_media":1023443,"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\/1023436"}],"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=1023436"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1023436\/revisions"}],"predecessor-version":[{"id":1023444,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1023436\/revisions\/1023444"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1023443"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1023436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1023436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1023436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}