{"id":1074222,"date":"2025-01-08T11:32:37","date_gmt":"2025-01-08T03:32:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074222.html"},"modified":"2025-01-08T11:32:39","modified_gmt":"2025-01-08T03:32:39","slug":"python%e5%a6%82%e4%bd%95%e6%89%b9%e9%87%8f%e6%8f%90%e4%ba%a4%e9%aa%8c%e8%af%81%e7%a0%81-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1074222.html","title":{"rendered":"python\u5982\u4f55\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103923\/a3d7b362-da1a-4d81-879d-c454a3e7edef.webp\" alt=\"python\u5982\u4f55\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801\" \/><\/p>\n<p><p> <strong>\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177\u3001\u6a21\u62df\u7528\u6237\u884c\u4e3a\u3001\u5b9e\u73b0\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u64cd\u4f5c\u3001\u4f7f\u7528\u56fe\u50cf\u8bc6\u522b\u6280\u672f\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u56fe\u50cf\u8bc6\u522b\u6280\u672f\u548c\u5b9e\u73b0\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u64cd\u4f5c\u662f\u6700\u4e3a\u5173\u952e\u7684\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Python\u6765\u5b9e\u73b0\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177<\/p>\n<\/p>\n<p><p>Python\u62e5\u6709\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u5de5\u5177\u5e93\uff0c\u5982Selenium\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u884c\u4e3a\uff0c\u5305\u62ec\u586b\u5199\u8868\u5355\u3001\u70b9\u51fb\u6309\u94ae\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u52a8\u5316\u5730\u8bbf\u95ee\u7f51\u9875\u5e76\u63d0\u4ea4\u9a8c\u8bc1\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Selenium\u81ea\u52a8\u5316\u63d0\u4ea4\u9a8c\u8bc1\u7801<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Selenium\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Selenium\u5e93\u548c\u76f8\u5e94\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Selenium\u6765\u5b9e\u73b0\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801\u7684\u793a\u4f8b\uff1a<\/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.common.keys import Keys<\/p>\n<p>import time<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6d4f\u89c8\u5668\u9a71\u52a8<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;path\/to\/chromedriver&#39;)<\/p>\n<h2><strong>\u8bbf\u95ee\u76ee\u6807\u7f51\u7ad9<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<h2><strong>\u627e\u5230\u9a8c\u8bc1\u7801\u8f93\u5165\u6846\u548c\u63d0\u4ea4\u6309\u94ae<\/strong><\/h2>\n<p>captcha_input = driver.find_element(By.ID, &#39;captcha&#39;)<\/p>\n<p>submit_button = driver.find_element(By.ID, &#39;submit&#39;)<\/p>\n<h2><strong>\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801<\/strong><\/h2>\n<p>captchas = [&#39;captcha1&#39;, &#39;captcha2&#39;, &#39;captcha3&#39;]<\/p>\n<p>for captcha in captchas:<\/p>\n<p>    # \u8f93\u5165\u9a8c\u8bc1\u7801<\/p>\n<p>    captcha_input.send_keys(captcha)<\/p>\n<p>    time.sleep(1)  # \u7b49\u5f851\u79d2\u4ee5\u6a21\u62df\u7528\u6237\u8f93\u5165<\/p>\n<p>    # \u63d0\u4ea4\u8868\u5355<\/p>\n<p>    submit_button.click()<\/p>\n<p>    time.sleep(2)  # \u7b49\u5f852\u79d2\u4ee5\u6a21\u62df\u7f51\u7edc\u5ef6\u8fdf<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u6a21\u62df\u7528\u6237\u884c\u4e3a<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9a8c\u8bc1\u7801\u7684\u63d0\u4ea4\u53ef\u80fd\u9700\u8981\u6a21\u62df\u66f4\u590d\u6742\u7684\u7528\u6237\u884c\u4e3a\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PyAutoGUI\u5e93\u6765\u5b9e\u73b0\u66f4\u9ad8\u7ea7\u7684\u81ea\u52a8\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528PyAutoGUI\u6a21\u62df\u7528\u6237\u884c\u4e3a<\/h3>\n<\/p>\n<p><p>PyAutoGUI\u5141\u8bb8\u6211\u4eec\u63a7\u5236\u9f20\u6807\u548c\u952e\u76d8\uff0c\u6a21\u62df\u771f\u5b9e\u7684\u7528\u6237\u884c\u4e3a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528PyAutoGUI\u63d0\u4ea4\u9a8c\u8bc1\u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p>import time<\/p>\n<h2><strong>\u6253\u5f00\u76ee\u6807\u7f51\u7ad9<\/strong><\/h2>\n<p>pyautogui.hotkey(&#39;ctrl&#39;, &#39;l&#39;)<\/p>\n<p>time.sleep(1)<\/p>\n<p>pyautogui.typewrite(&#39;http:\/\/example.com&#39;, interval=0.1)<\/p>\n<p>pyautogui.press(&#39;enter&#39;)<\/p>\n<p>time.sleep(5)  # \u7b49\u5f85\u7f51\u9875\u52a0\u8f7d<\/p>\n<h2><strong>\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801<\/strong><\/h2>\n<p>captchas = [&#39;captcha1&#39;, &#39;captcha2&#39;, &#39;captcha3&#39;]<\/p>\n<p>for captcha in captchas:<\/p>\n<p>    # \u8f93\u5165\u9a8c\u8bc1\u7801<\/p>\n<p>    pyautogui.typewrite(captcha, interval=0.1)<\/p>\n<p>    time.sleep(1)  # \u7b49\u5f851\u79d2\u4ee5\u6a21\u62df\u7528\u6237\u8f93\u5165<\/p>\n<p>    # \u63d0\u4ea4\u8868\u5355<\/p>\n<p>    pyautogui.press(&#39;enter&#39;)<\/p>\n<p>    time.sleep(2)  # \u7b49\u5f852\u79d2\u4ee5\u6a21\u62df\u7f51\u7edc\u5ef6\u8fdf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5b9e\u73b0\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u9a8c\u8bc1\u7801\u63d0\u4ea4\u7684\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u64cd\u4f5c\u6765\u5e76\u884c\u5904\u7406\u591a\u4e2a\u63d0\u4ea4\u8bf7\u6c42\u3002Python\u4e2d\u7684<code>threading<\/code>\u5e93\u548c<code>asyncio<\/code>\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Threading\u5e93\u5b9e\u73b0\u591a\u7ebf\u7a0b\u63d0\u4ea4<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Threading\u5e93\u5b9e\u73b0\u591a\u7ebf\u7a0b\u63d0\u4ea4\u9a8c\u8bc1\u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import requests<\/p>\n<p>def submit_captcha(captcha):<\/p>\n<p>    url = &#39;http:\/\/example.com\/submit&#39;<\/p>\n<p>    data = {&#39;captcha&#39;: captcha}<\/p>\n<p>    response = requests.post(url, data=data)<\/p>\n<p>    print(response.text)<\/p>\n<p>captchas = [&#39;captcha1&#39;, &#39;captcha2&#39;, &#39;captcha3&#39;]<\/p>\n<p>threads = []<\/p>\n<p>for captcha in captchas:<\/p>\n<p>    thread = threading.Thread(target=submit_captcha, args=(captcha,))<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528Asyncio\u5e93\u5b9e\u73b0\u5f02\u6b65\u63d0\u4ea4<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Asyncio\u5e93\u5b9e\u73b0\u5f02\u6b65\u63d0\u4ea4\u9a8c\u8bc1\u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import <a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ohttp<\/p>\n<p>import asyncio<\/p>\n<p>async def submit_captcha(session, captcha):<\/p>\n<p>    url = &#39;http:\/\/example.com\/submit&#39;<\/p>\n<p>    data = {&#39;captcha&#39;: captcha}<\/p>\n<p>    async with session.post(url, data=data) as response:<\/p>\n<p>        print(await response.text())<\/p>\n<p>async def main():<\/p>\n<p>    captchas = [&#39;captcha1&#39;, &#39;captcha2&#39;, &#39;captcha3&#39;]<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        tasks = [submit_captcha(session, captcha) for captcha in captchas]<\/p>\n<p>        await asyncio.gather(*tasks)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u56fe\u50cf\u8bc6\u522b\u6280\u672f<\/p>\n<\/p>\n<p><p>\u9a8c\u8bc1\u7801\u901a\u5e38\u662f\u56fe\u50cf\u5f62\u5f0f\u7684\uff0c\u4e3a\u4e86\u81ea\u52a8\u5316\u63d0\u4ea4\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u56fe\u50cf\u8bc6\u522b\u6280\u672f\u6765\u89e3\u6790\u9a8c\u8bc1\u7801\u56fe\u50cf\u3002Python\u4e2d\u7684Pillow\u5e93\u548cTesseract OCR\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Pillow\u548cTesseract\u89e3\u6790\u9a8c\u8bc1\u7801<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Pillow\u5e93\u548cTesseract OCR\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u89e3\u6790\u9a8c\u8bc1\u7801\u5e76\u63d0\u4ea4\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import pytesseract<\/p>\n<p>import requests<\/p>\n<p>def parse_captcha(image_path):<\/p>\n<p>    image = Image.open(image_path)<\/p>\n<p>    captcha = pytesseract.image_to_string(image)<\/p>\n<p>    return captcha.strip()<\/p>\n<p>def submit_captcha(captcha):<\/p>\n<p>    url = &#39;http:\/\/example.com\/submit&#39;<\/p>\n<p>    data = {&#39;captcha&#39;: captcha}<\/p>\n<p>    response = requests.post(url, data=data)<\/p>\n<p>    print(response.text)<\/p>\n<p>captcha_images = [&#39;captcha1.png&#39;, &#39;captcha2.png&#39;, &#39;captcha3.png&#39;]<\/p>\n<p>for image_path in captcha_images:<\/p>\n<p>    captcha = parse_captcha(image_path)<\/p>\n<p>    submit_captcha(captcha)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801\u7684\u4efb\u52a1\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u81ea\u52a8\u5316\u64cd\u4f5c\u3002\u6ce8\u610f\uff0c\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801\u53ef\u80fd\u8fdd\u53cd\u67d0\u4e9b\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u8bf7\u786e\u4fdd\u5728\u5408\u6cd5\u5408\u89c4\u7684\u524d\u63d0\u4e0b\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u81ea\u52a8\u5316\u63d0\u4ea4\u9a8c\u8bc1\u7801\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884c\u9a8c\u8bc1\u7801\u81ea\u52a8\u63d0\u4ea4\u901a\u5e38\u6d89\u53ca\u5230\u7f51\u9875\u8bf7\u6c42\u548c\u56fe\u50cf\u8bc6\u522b\u6280\u672f\u3002\u53ef\u4ee5\u5229\u7528\u5e93\u5982<code>requests<\/code>\u8fdb\u884cHTTP\u8bf7\u6c42\uff0c\u7ed3\u5408<code>Pillow<\/code>\u548c<code>pytesseract<\/code>\u8fdb\u884c\u9a8c\u8bc1\u7801\u7684\u8bc6\u522b\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8bb8\u591a\u7f51\u7ad9\u4f1a\u4f7f\u7528\u9632\u722c\u866b\u673a\u5236\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4ee3\u7406\u6216\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\u7684\u5e93\uff0c\u5982<code>Selenium<\/code>\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801\u9700\u8981\u54ea\u4e9b\u5de5\u5177\u6216\u5e93\uff1f<\/strong><br \/>\u5e38\u7528\u7684\u5de5\u5177\u548c\u5e93\u5305\u62ec<code>requests<\/code>\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c<code>BeautifulSoup<\/code>\u7528\u4e8e\u89e3\u6790\u7f51\u9875\u5185\u5bb9\uff0c<code>Selenium<\/code>\u7528\u4e8e\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c<code>Pillow<\/code>\u548c<code>pytesseract<\/code>\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u9a8c\u8bc1\u7801\u8bc6\u522b\u3002\u6b64\u5916\uff0c\u53ef\u80fd\u8fd8\u9700\u8981<code>numpy<\/code>\u548c<code>opencv<\/code>\u7b49\u5e93\u6765\u5904\u7406\u590d\u6742\u7684\u9a8c\u8bc1\u7801\u56fe\u50cf\u3002<\/p>\n<p><strong>\u5728\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801\u65f6\uff0c\u5982\u4f55\u5904\u7406\u8bc6\u522b\u9519\u8bef\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u9a8c\u8bc1\u7801\u8bc6\u522b\u9519\u8bef\u662f\u5e38\u89c1\u7684\u95ee\u9898\u3002\u53ef\u4ee5\u91c7\u53d6\u4e00\u4e9b\u7b56\u7565\u6765\u63d0\u9ad8\u8bc6\u522b\u51c6\u786e\u7387\uff0c\u6bd4\u5982\u9884\u5904\u7406\u56fe\u50cf\uff08\u4f8b\u5982\u53bb\u566a\u58f0\u3001\u8c03\u6574\u5bf9\u6bd4\u5ea6\uff09\uff0c\u4f7f\u7528\u66f4\u5f3a\u5927\u7684OCR\u5de5\u5177\uff0c\u6216\u8005\u901a\u8fc7<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u8bad\u7ec3\u81ea\u5df1\u7684\u6a21\u578b\u6765\u9002\u5e94\u7279\u5b9a\u7f51\u7ad9\u7684\u9a8c\u8bc1\u7801\u3002\u6b64\u5916\uff0c\u8bbe\u7f6e\u91cd\u8bd5\u673a\u5236\uff0c\u5f53\u8bc6\u522b\u9519\u8bef\u65f6\u53ef\u4ee5\u81ea\u52a8\u91cd\u65b0\u63d0\u4ea4\u9a8c\u8bc1\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6279\u91cf\u63d0\u4ea4\u9a8c\u8bc1\u7801\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177\u3001\u6a21\u62df\u7528\u6237\u884c\u4e3a\u3001\u5b9e\u73b0\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u64cd\u4f5c\u3001\u4f7f\u7528\u56fe\u50cf\u8bc6\u522b\u6280\u672f\u3002 \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1074227,"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\/1074222"}],"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=1074222"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074222\/revisions"}],"predecessor-version":[{"id":1074230,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074222\/revisions\/1074230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1074227"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1074222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1074222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1074222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}