{"id":1026934,"date":"2024-12-31T10:46:51","date_gmt":"2024-12-31T02:46:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026934.html"},"modified":"2024-12-31T10:46:53","modified_gmt":"2024-12-31T02:46:53","slug":"python%e5%a6%82%e4%bd%95%e6%a8%a1%e6%8b%9f%e7%99%bb%e9%99%86%e7%bd%91%e7%ab%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026934.html","title":{"rendered":"python\u5982\u4f55\u6a21\u62df\u767b\u9646\u7f51\u7ad9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/9f48b213-3258-4ef0-837c-355cb98c189c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u6a21\u62df\u767b\u9646\u7f51\u7ad9\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u6a21\u62df\u767b\u5f55\u7f51\u7ad9\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u5904\u7406Cookies\u3001\u63d0\u4ea4\u8868\u5355\u7b49\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u4e86\u89e3\u76ee\u6807\u7f51\u7ad9\u7684\u767b\u5f55\u673a\u5236\u548c\u8bf7\u6c42\u6d41\u7a0b\u3002\u63a5\u4e0b\u6765\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecrequests\u3001BeautifulSoup\u3001Selenium\u7b49\u3002\u4ee5\u4e0b\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528requests\u5e93\u6765\u6a21\u62df\u767b\u5f55\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4e86\u89e3\u76ee\u6807\u7f51\u7ad9\u7684\u767b\u5f55\u6d41\u7a0b<\/p>\n<p>\u5728\u6a21\u62df\u767b\u5f55\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u4e86\u89e3\u76ee\u6807\u7f51\u7ad9\u7684\u767b\u5f55\u673a\u5236\u3002\u53ef\u4ee5\u4f7f\u7528\u6d4f\u89c8\u5668\u7684\u5f00\u53d1\u8005\u5de5\u5177\uff08F12\uff09\u67e5\u770b\u7f51\u7edc\u8bf7\u6c42\uff0c\u627e\u51fa\u8868\u5355\u63d0\u4ea4\u7684\u5730\u5740\uff08\u5373POST\u8bf7\u6c42\u7684URL\uff09\u3001\u8868\u5355\u6570\u636e\uff08\u5982\u7528\u6237\u540d\u3001\u5bc6\u7801\uff09\u548c\u53ef\u80fd\u9700\u8981\u7684Cookies\u6216Headers\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528requests\u5e93\u6a21\u62df\u767b\u5f55<\/p>\n<p>requests\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684HTTP\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u53d1\u9001GET\u548cPOST\u8bf7\u6c42\u3002\u901a\u8fc7POST\u8bf7\u6c42\u63d0\u4ea4\u767b\u5f55\u8868\u5355\u6570\u636e\uff0c\u5e76\u5904\u7406\u8fd4\u56de\u7684Cookies\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2asession\u5bf9\u8c61<\/strong><\/h2>\n<p>session = requests.Session()<\/p>\n<h2><strong>\u76ee\u6807\u7f51\u7ad9\u7684\u767b\u5f55URL<\/strong><\/h2>\n<p>login_url = &quot;https:\/\/example.com\/login&quot;<\/p>\n<h2><strong>\u6784\u9020\u8868\u5355\u6570\u636e<\/strong><\/h2>\n<p>payload = {<\/p>\n<p>    &quot;username&quot;: &quot;your_username&quot;,<\/p>\n<p>    &quot;password&quot;: &quot;your_password&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001POST\u8bf7\u6c42\uff0c\u63d0\u4ea4\u8868\u5355\u6570\u636e<\/strong><\/h2>\n<p>response = session.post(login_url, data=payload)<\/p>\n<h2><strong>\u68c0\u67e5\u767b\u5f55\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if &quot;Welcome&quot; in response.text:<\/p>\n<p>    print(&quot;\u767b\u5f55\u6210\u529f\uff01&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u767b\u5f55\u5931\u8d25\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efaSession\u5bf9\u8c61<\/strong>\uff1aSession\u5bf9\u8c61\u53ef\u4ee5\u8de8\u8bf7\u6c42\u4fdd\u5b58Cookies\uff0c\u5e76\u4e14\u5728\u540c\u4e00\u4e2aSession\u4e2d\u4f1a\u8bdd\u3002<\/li>\n<li><strong>\u6784\u9020\u8868\u5355\u6570\u636e<\/strong>\uff1a\u901a\u8fc7\u89c2\u5bdf\u6d4f\u89c8\u5668\u7684\u5f00\u53d1\u8005\u5de5\u5177\uff0c\u627e\u5230\u767b\u5f55\u8868\u5355\u9700\u8981\u63d0\u4ea4\u7684\u5b57\u6bb5\uff0c\u5e76\u6784\u9020payload\u3002<\/li>\n<li><strong>\u53d1\u9001POST\u8bf7\u6c42<\/strong>\uff1a\u4f7f\u7528session\u5bf9\u8c61\u53d1\u9001POST\u8bf7\u6c42\uff0c\u63d0\u4ea4\u8868\u5355\u6570\u636e\u3002<\/li>\n<li><strong>\u68c0\u67e5\u767b\u5f55\u72b6\u6001<\/strong>\uff1a\u901a\u8fc7\u68c0\u67e5\u54cd\u5e94\u5185\u5bb9\uff08response.text\uff09\u6765\u5224\u65ad\u767b\u5f55\u662f\u5426\u6210\u529f\u3002<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5904\u7406Cookies\u548cHeaders<\/p>\n<p>\u6709\u4e9b\u7f51\u7ad9\u53ef\u80fd\u4f1a\u4f7f\u7528Cookies\u6216\u7279\u5b9a\u7684Headers\uff08\u5982User-Agent\uff09\u6765\u9a8c\u8bc1\u8bf7\u6c42\u3002\u4f60\u53ef\u4ee5\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\u6dfb\u52a0\u8fd9\u4e9b\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">headers = {<\/p>\n<p>    &quot;User-Agent&quot;: &quot;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/58.0.3029.110 Safari\/537.3&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001POST\u8bf7\u6c42\u65f6\u6dfb\u52a0Headers<\/strong><\/h2>\n<p>response = session.post(login_url, data=payload, headers=headers)<\/p>\n<h2><strong>\u5904\u7406Cookies<\/strong><\/h2>\n<p>cookies = response.cookies<\/p>\n<h2><strong>\u53d1\u9001\u5176\u4ed6\u8bf7\u6c42\u65f6\u9644\u5e26Cookies<\/strong><\/h2>\n<p>response = session.get(&quot;https:\/\/example.com\/protected_page&quot;, cookies=cookies)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u6dfb\u52a0Headers<\/strong>\uff1a\u6784\u9020\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u9700\u8981\u7684Headers\u4fe1\u606f\uff0c\u5e76\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\u4f20\u5165\u3002<\/li>\n<li><strong>\u5904\u7406Cookies<\/strong>\uff1a\u83b7\u53d6\u767b\u5f55\u8bf7\u6c42\u8fd4\u56de\u7684Cookies\uff0c\u5e76\u5728\u540e\u7eed\u8bf7\u6c42\u4e2d\u9644\u5e26\u8fd9\u4e9bCookies\u3002<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790\u54cd\u5e94\u5185\u5bb9<\/p>\n<p>\u5982\u679c\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u767b\u5f55\u540e\u7684\u9875\u9762\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790HTML\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42\u83b7\u53d6\u767b\u5f55\u540e\u7684\u9875\u9762<\/strong><\/h2>\n<p>response = session.get(&quot;https:\/\/example.com\/protected_page&quot;)<\/p>\n<h2><strong>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.text, &quot;html.parser&quot;)<\/p>\n<h2><strong>\u67e5\u627e\u9700\u8981\u7684\u4fe1\u606f<\/strong><\/h2>\n<p>data = soup.find(&quot;div&quot;, {&quot;class&quot;: &quot;data&quot;}).text<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong>\uff1a\u4f7f\u7528session\u5bf9\u8c61\u53d1\u9001GET\u8bf7\u6c42\uff0c\u83b7\u53d6\u9700\u8981\u89e3\u6790\u7684\u9875\u9762\u3002<\/li>\n<li><strong>\u89e3\u6790HTML<\/strong>\uff1a\u4f7f\u7528BeautifulSoup\u89e3\u6790\u54cd\u5e94\u5185\u5bb9\uff0c\u5e76\u67e5\u627e\u9700\u8981\u7684\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5904\u7406\u590d\u6742\u7684\u767b\u5f55\u6d41\u7a0b<\/p>\n<p>\u6709\u4e9b\u7f51\u7ad9\u7684\u767b\u5f55\u6d41\u7a0b\u6bd4\u8f83\u590d\u6742\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u91cd\u5b9a\u5411\u3001\u9a8c\u8bc1\u7801\u3001JavaScript\u52a8\u6001\u52a0\u8f7d\u7b49\u60c5\u51b5\u3002\u8fd9\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Selenium\u5e93\uff0c\u901a\u8fc7\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u521b\u5efa\u6d4f\u89c8\u5668\u5bf9\u8c61<\/strong><\/h2>\n<p>browser = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u767b\u5f55\u9875\u9762<\/strong><\/h2>\n<p>browser.get(&quot;https:\/\/example.com\/login&quot;)<\/p>\n<h2><strong>\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801<\/strong><\/h2>\n<p>browser.find_element_by_name(&quot;username&quot;).send_keys(&quot;your_username&quot;)<\/p>\n<p>browser.find_element_by_name(&quot;password&quot;).send_keys(&quot;your_password&quot;)<\/p>\n<h2><strong>\u63d0\u4ea4\u8868\u5355<\/strong><\/h2>\n<p>browser.find_element_by_name(&quot;submit&quot;).click()<\/p>\n<h2><strong>\u83b7\u53d6\u767b\u5f55\u540e\u7684\u9875\u9762\u5185\u5bb9<\/strong><\/h2>\n<p>html = browser.page_source<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>browser.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u6d4f\u89c8\u5668\u5bf9\u8c61<\/strong>\uff1a\u4f7f\u7528Selenium\u9a71\u52a8\u6d4f\u89c8\u5668\uff08\u5982Chrome\uff09\u3002<\/li>\n<li><strong>\u6253\u5f00\u767b\u5f55\u9875\u9762<\/strong>\uff1a\u901a\u8fc7get\u65b9\u6cd5\u6253\u5f00\u76ee\u6807\u7f51\u7ad9\u7684\u767b\u5f55\u9875\u9762\u3002<\/li>\n<li><strong>\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801<\/strong>\uff1a\u4f7f\u7528find_element_by_name\u65b9\u6cd5\u627e\u5230\u8868\u5355\u5143\u7d20\uff0c\u5e76\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/li>\n<li><strong>\u63d0\u4ea4\u8868\u5355<\/strong>\uff1a\u6a21\u62df\u70b9\u51fb\u63d0\u4ea4\u6309\u94ae\u3002<\/li>\n<li><strong>\u83b7\u53d6\u9875\u9762\u5185\u5bb9<\/strong>\uff1a\u83b7\u53d6\u767b\u5f55\u540e\u7684\u9875\u9762\u5185\u5bb9\u3002<\/li>\n<li><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong>\uff1a\u5173\u95ed\u6d4f\u89c8\u5668\u3002<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5904\u7406\u9a8c\u8bc1\u7801<\/p>\n<p>\u6709\u4e9b\u7f51\u7ad9\u5728\u767b\u5f55\u65f6\u4f1a\u8981\u6c42\u8f93\u5165\u9a8c\u8bc1\u7801\u3002\u5904\u7406\u9a8c\u8bc1\u7801\u901a\u5e38\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u624b\u52a8\u8f93\u5165\u548c\u4f7f\u7528OCR\u6280\u672f\u81ea\u52a8\u8bc6\u522b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u624b\u52a8\u8f93\u5165<\/strong>\uff1a\u5f53\u9047\u5230\u9a8c\u8bc1\u7801\u65f6\uff0c\u6682\u505c\u7a0b\u5e8f\uff0c\u63d0\u793a\u7528\u6237\u8f93\u5165\u9a8c\u8bc1\u7801\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from PIL import Image<\/p>\n<p>from io import BytesIO<\/p>\n<h2><strong>\u521b\u5efasession\u5bf9\u8c61<\/strong><\/h2>\n<p>session = requests.Session()<\/p>\n<h2><strong>\u76ee\u6807\u7f51\u7ad9\u7684\u767b\u5f55URL<\/strong><\/h2>\n<p>login_url = &quot;https:\/\/example.com\/login&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u9a8c\u8bc1\u7801\u56fe\u7247<\/strong><\/h2>\n<p>captcha_url = &quot;https:\/\/example.com\/captcha&quot;<\/p>\n<p>response = session.get(captcha_url)<\/p>\n<p>img = Image.open(BytesIO(response.content))<\/p>\n<p>img.show()<\/p>\n<h2><strong>\u624b\u52a8\u8f93\u5165\u9a8c\u8bc1\u7801<\/strong><\/h2>\n<p>captcha_code = input(&quot;\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801\uff1a&quot;)<\/p>\n<h2><strong>\u6784\u9020\u8868\u5355\u6570\u636e<\/strong><\/h2>\n<p>payload = {<\/p>\n<p>    &quot;username&quot;: &quot;your_username&quot;,<\/p>\n<p>    &quot;password&quot;: &quot;your_password&quot;,<\/p>\n<p>    &quot;captcha&quot;: captcha_code<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001POST\u8bf7\u6c42\uff0c\u63d0\u4ea4\u8868\u5355\u6570\u636e<\/strong><\/h2>\n<p>response = session.post(login_url, data=payload)<\/p>\n<h2><strong>\u68c0\u67e5\u767b\u5f55\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if &quot;Welcome&quot; in response.text:<\/p>\n<p>    print(&quot;\u767b\u5f55\u6210\u529f\uff01&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u767b\u5f55\u5931\u8d25\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528OCR\u6280\u672f\u81ea\u52a8\u8bc6\u522b<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528Tesseract OCR\u7b49\u5e93\u81ea\u52a8\u8bc6\u522b\u9a8c\u8bc1\u7801\uff08\u5bf9\u4e8e\u7b80\u5355\u7684\u9a8c\u8bc1\u7801\u6709\u6548\uff09\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pytesseract<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u4e0b\u8f7d\u5e76\u5b89\u88c5Tesseract OCR<\/strong><\/h2>\n<h2><strong>https:\/\/github.com\/tesseract-ocr\/tesseract<\/strong><\/h2>\n<h2><strong>\u83b7\u53d6\u9a8c\u8bc1\u7801\u56fe\u7247<\/strong><\/h2>\n<p>captcha_url = &quot;https:\/\/example.com\/captcha&quot;<\/p>\n<p>response = session.get(captcha_url)<\/p>\n<p>img = Image.open(BytesIO(response.content))<\/p>\n<h2><strong>\u4f7f\u7528OCR\u8bc6\u522b\u9a8c\u8bc1\u7801<\/strong><\/h2>\n<p>captcha_code = pytesseract.image_to_string(img)<\/p>\n<p>print(&quot;\u8bc6\u522b\u7684\u9a8c\u8bc1\u7801\uff1a&quot;, captcha_code)<\/p>\n<h2><strong>\u6784\u9020\u8868\u5355\u6570\u636e<\/strong><\/h2>\n<p>payload = {<\/p>\n<p>    &quot;username&quot;: &quot;your_username&quot;,<\/p>\n<p>    &quot;password&quot;: &quot;your_password&quot;,<\/p>\n<p>    &quot;captcha&quot;: captcha_code<\/p>\n<p>}<\/p>\n<h2><strong>\u53d1\u9001POST\u8bf7\u6c42\uff0c\u63d0\u4ea4\u8868\u5355\u6570\u636e<\/strong><\/h2>\n<p>response = session.post(login_url, data=payload)<\/p>\n<h2><strong>\u68c0\u67e5\u767b\u5f55\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if &quot;Welcome&quot; in response.text:<\/p>\n<p>    print(&quot;\u767b\u5f55\u6210\u529f\uff01&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u767b\u5f55\u5931\u8d25\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u624b\u52a8\u8f93\u5165\u9a8c\u8bc1\u7801<\/strong>\uff1a\u83b7\u53d6\u9a8c\u8bc1\u7801\u56fe\u7247\u5e76\u663e\u793a\uff0c\u63d0\u793a\u7528\u6237\u624b\u52a8\u8f93\u5165\u9a8c\u8bc1\u7801\u3002<\/li>\n<li><strong>\u4f7f\u7528OCR\u8bc6\u522b\u9a8c\u8bc1\u7801<\/strong>\uff1a\u4f7f\u7528Tesseract OCR\u8bc6\u522b\u9a8c\u8bc1\u7801\u56fe\u7247\uff0c\u81ea\u52a8\u586b\u5165\u8868\u5355\u3002<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u5904\u7406JavaScript\u52a8\u6001\u52a0\u8f7d<\/p>\n<p>\u6709\u4e9b\u7f51\u7ad9\u4f7f\u7528JavaScript\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\uff0c\u76f4\u63a5\u4f7f\u7528requests\u5e93\u65e0\u6cd5\u83b7\u53d6\u5b8c\u6574\u7684\u9875\u9762\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528Selenium\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u521b\u5efa\u6d4f\u89c8\u5668\u5bf9\u8c61<\/strong><\/h2>\n<p>browser = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u767b\u5f55\u9875\u9762<\/strong><\/h2>\n<p>browser.get(&quot;https:\/\/example.com\/login&quot;)<\/p>\n<h2><strong>\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801<\/strong><\/h2>\n<p>browser.find_element_by_name(&quot;username&quot;).send_keys(&quot;your_username&quot;)<\/p>\n<p>browser.find_element_by_name(&quot;password&quot;).send_keys(&quot;your_password&quot;)<\/p>\n<h2><strong>\u63d0\u4ea4\u8868\u5355<\/strong><\/h2>\n<p>browser.find_element_by_name(&quot;submit&quot;).click()<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/strong><\/h2>\n<p>browser.implicitly_w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t(10)  # \u7b49\u5f8510\u79d2<\/p>\n<h2><strong>\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>html = browser.page_source<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>browser.quit()<\/p>\n<h2><strong>\u89e3\u6790HTML<\/strong><\/h2>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(html, &quot;html.parser&quot;)<\/p>\n<p>data = soup.find(&quot;div&quot;, {&quot;class&quot;: &quot;data&quot;}).text<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/strong>\uff1a\u4f7f\u7528implicitly_wait\u65b9\u6cd5\u8bbe\u7f6e\u9690\u5f0f\u7b49\u5f85\u65f6\u95f4\uff0c\u786e\u4fdd\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u3002<\/li>\n<li><strong>\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9<\/strong>\uff1a\u83b7\u53d6\u9875\u9762\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528BeautifulSoup\u89e3\u6790\u3002<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u603b\u7ed3<\/p>\n<p>\u4f7f\u7528Python\u6a21\u62df\u767b\u5f55\u7f51\u7ad9\u6d89\u53ca\u591a\u4e2a\u6b65\u9aa4\uff0c\u5305\u62ec\u4e86\u89e3\u767b\u5f55\u6d41\u7a0b\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u5904\u7406Cookies\u548cHeaders\u3001\u89e3\u6790\u54cd\u5e94\u5185\u5bb9\u7b49\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u767b\u5f55\u6d41\u7a0b\uff0crequests\u5e93\u5df2\u7ecf\u8db3\u591f\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u767b\u5f55\u6d41\u7a0b\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528Selenium\u5e93\u3002\u5904\u7406\u9a8c\u8bc1\u7801\u548cJavaScript\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\u65f6\uff0c\u9700\u8981\u989d\u5916\u7684\u6280\u672f\u624b\u6bb5\uff08\u5982OCR\u3001\u9690\u5f0f\u7b49\u5f85\uff09\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u4e0d\u65ad\u5b9e\u8df5\u548c\u603b\u7ed3\u7ecf\u9a8c\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1\u8fd9\u4e00\u6280\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6a21\u62df\u767b\u5f55\u7f51\u7ad9\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u6a21\u62df\u767b\u5f55\u7f51\u7ad9\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528requests\u5e93\u548cBeautifulSoup\u5e93\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u67e5\u770b\u7f51\u7ad9\u7684\u767b\u5f55\u8868\u5355\uff0c\u786e\u5b9a\u6240\u9700\u7684\u5b57\u6bb5\uff08\u5982\u7528\u6237\u540d\u3001\u5bc6\u7801\u7b49\uff09\u4ee5\u53ca\u767b\u5f55\u7684URL\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u53d1\u9001POST\u8bf7\u6c42\uff0c\u5c06\u8868\u5355\u6570\u636e\u4f20\u9012\u7ed9\u670d\u52a1\u5668\u3002\u5982\u679c\u767b\u5f55\u6210\u529f\uff0c\u60a8\u5c06\u83b7\u5f97\u4e00\u4e2a\u4f1a\u8bddcookie\uff0c\u4ee5\u4fbf\u5728\u540e\u7eed\u8bf7\u6c42\u4e2d\u4fdd\u6301\u767b\u5f55\u72b6\u6001\u3002<\/p>\n<p><strong>\u6a21\u62df\u767b\u5f55\u65f6\uff0c\u5982\u4f55\u5904\u7406\u9a8c\u8bc1\u7801\u548c\u5b89\u5168\u9a8c\u8bc1\uff1f<\/strong><br \/>\u5f88\u591a\u7f51\u7ad9\u4f7f\u7528\u9a8c\u8bc1\u7801\u6765\u9632\u6b62\u673a\u5668\u4eba\u767b\u5f55\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u60c5\u51b5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u670d\u52a1\u6216\u5de5\u5177\uff08\u5982OCR\u5e93\uff09\u6765\u8bc6\u522b\u9a8c\u8bc1\u7801\u3002\u5982\u679c\u7f51\u7ad9\u4f7f\u7528\u66f4\u590d\u6742\u7684\u5b89\u5168\u9a8c\u8bc1\u673a\u5236\uff0c\u5982\u53cc\u56e0\u7d20\u8ba4\u8bc1\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u624b\u52a8\u8f93\u5165\u9a8c\u8bc1\u7801\uff0c\u6216\u8005\u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177\u5982Selenium\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\u3002<\/p>\n<p><strong>\u54ea\u4e9b\u5e93\u6216\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u7f51\u9875\u81ea\u52a8\u5316\u548c\u6a21\u62df\u767b\u5f55\uff1f<\/strong><br \/>\u9664\u4e86requests\u548cBeautifulSoup\uff0cSelenium\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u64cd\u4f5c\uff0c\u5305\u62ec\u767b\u5f55\u3002Selenium\u652f\u6301\u591a\u79cd\u6d4f\u89c8\u5668\uff0c\u80fd\u591f\u5904\u7406JavaScript\u751f\u6210\u7684\u5185\u5bb9\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u8fdb\u884c\u590d\u6742\u4ea4\u4e92\u7684\u7f51\u7ad9\u3002\u6b64\u5916\uff0cScrapy\u4e5f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6846\u67b6\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6293\u53d6\u548c\u81ea\u52a8\u5316\u4efb\u52a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u6a21\u62df\u767b\u5f55\u7f51\u7ad9\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u5904\u7406Cookies\u3001\u63d0\u4ea4\u8868\u5355\u7b49\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u4e86 [&hellip;]","protected":false},"author":3,"featured_media":1026944,"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\/1026934"}],"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=1026934"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026934\/revisions"}],"predecessor-version":[{"id":1026945,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026934\/revisions\/1026945"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1026944"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1026934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1026934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1026934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}