{"id":1160943,"date":"2025-01-13T19:10:55","date_gmt":"2025-01-13T11:10:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160943.html"},"modified":"2025-01-13T19:10:58","modified_gmt":"2025-01-13T11:10:58","slug":"python%e5%a6%82%e4%bd%95%e7%ad%89%e5%be%85%e5%9b%be%e7%89%87%e5%87%ba%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160943.html","title":{"rendered":"python\u5982\u4f55\u7b49\u5f85\u56fe\u7247\u51fa\u73b0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202323\/1db26837-d865-43e2-adb8-a31c2910270b.webp\" alt=\"python\u5982\u4f55\u7b49\u5f85\u56fe\u7247\u51fa\u73b0\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7b49\u5f85\u56fe\u7247\u51fa\u73b0\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982\u5229\u7528Selenium\u3001Pillow\u3001OpenCV\u7b49\u5de5\u5177\u3002<\/strong> \u6700\u63a8\u8350\u7684\u65b9\u5f0f\u662f\u4f7f\u7528Selenium\u5e93\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u5e76\u4e14\u53ef\u4ee5\u7ed3\u5408WebDriverW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t\u7c7b\u6765\u7b49\u5f85\u56fe\u7247\u7684\u52a0\u8f7d\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u521d\u59cb\u5316WebDriver\u3001\u52a0\u8f7d\u9875\u9762\u3001\u4f7f\u7528\u663e\u5f0f\u7b49\u5f85\u6765\u68c0\u6d4b\u56fe\u7247\u5143\u7d20\u7b49\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u548c\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Selenium\u7b49\u5f85\u56fe\u7247\u51fa\u73b0<\/p>\n<p>Selenium\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u81ea\u52a8\u5316\u6d4b\u8bd5\u3001\u7f51\u9875\u6293\u53d6\u7b49\u4efb\u52a1\u4e2d\u3002\u901a\u8fc7\u7ed3\u5408Selenium\u548cWebDriverWait\u7c7b\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7b49\u5f85\u56fe\u7247\u52a0\u8f7d\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5Selenium<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Selenium\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><h3>2. \u521d\u59cb\u5316WebDriver<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Selenium\u4e4b\u524d\uff0c\u9700\u8981\u521d\u59cb\u5316WebDriver\u3002WebDriver\u662fSelenium\u5e93\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u7ec4\u4ef6\uff0c\u8d1f\u8d23\u63a7\u5236\u6d4f\u89c8\u5668\u7684\u884c\u4e3a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u521d\u59cb\u5316WebDriver\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aChrome\u6d4f\u89c8\u5668\u5b9e\u4f8b<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;\/path\/to\/chromedriver&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u6ce8\u610f\uff0c<code>\/path\/to\/chromedriver<\/code>\u9700\u8981\u66ff\u6362\u4e3a\u4f60\u672c\u5730\u7684chromedriver\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>3. \u52a0\u8f7d\u9875\u9762<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528WebDriver\u52a0\u8f7d\u4f60\u9700\u8981\u68c0\u6d4b\u7684\u7f51\u9875\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">url = &#39;https:\/\/example.com&#39;<\/p>\n<p>driver.get(url)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u4f7f\u7528\u663e\u5f0f\u7b49\u5f85<\/h3>\n<\/p>\n<p><p>\u663e\u5f0f\u7b49\u5f85\u662fSelenium\u5e93\u4e2d\u7684\u4e00\u4e2a\u529f\u80fd\uff0c\u5141\u8bb8\u4f60\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\u540e\u518d\u7ee7\u7eed\u6267\u884c\u811a\u672c\u3002\u5728\u7b49\u5f85\u56fe\u7247\u52a0\u8f7d\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>WebDriverWait<\/code>\u548c<code>expected_conditions<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.support.ui import WebDriverWait<\/p>\n<p>from selenium.webdriver.support import expected_conditions as EC<\/p>\n<h2><strong>\u8bbe\u7f6e\u6700\u957f\u7b49\u5f85\u65f6\u95f4\u4e3a10\u79d2<\/strong><\/h2>\n<p>wait = WebDriverWait(driver, 10)<\/p>\n<h2><strong>\u7b49\u5f85\u56fe\u7247\u5143\u7d20\u52a0\u8f7d\u5b8c\u6210<\/strong><\/h2>\n<p>image_element = wait.until(EC.presence_of_element_located((By.XPATH, &#39;\/\/img[@src=&quot;image_source_url&quot;]&#39;)))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>\/\/img[@src=&quot;image_source_url&quot;]<\/code>\u662f\u56fe\u7247\u5143\u7d20\u7684XPath\u8def\u5f84\uff0c\u4f60\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h3>5. \u5904\u7406\u56fe\u7247\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u56fe\u7247\u5143\u7d20\u52a0\u8f7d\u5b8c\u6210\uff0c\u4f60\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u4f8b\u5982\u83b7\u53d6\u56fe\u7247URL\u3001\u4e0b\u8f7d\u56fe\u7247\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">image_url = image_element.get_attribute(&#39;src&#39;)<\/p>\n<p>print(f&#39;Image URL: {image_url}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6. \u5173\u95ed\u6d4f\u89c8\u5668<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u8bb0\u5f97\u5173\u95ed\u6d4f\u89c8\u5668\u5b9e\u4f8b\u4ee5\u91ca\u653e\u8d44\u6e90\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Pillow\u548cOpenCV\u7b49\u5f85\u56fe\u7247\u51fa\u73b0<\/p>\n<p>\u9664\u4e86Selenium\u4e4b\u5916\uff0cPillow\u548cOpenCV\u4e5f\u662f\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e24\u4e2a\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u672c\u5730\u56fe\u7247\u7684\u68c0\u6d4b\u548c\u7b49\u5f85\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5Pillow\u548cOpenCV<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pillow\u548cOpenCV\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 pillow opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528Pillow\u52a0\u8f7d\u56fe\u7247<\/h3>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\u7684\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u7247\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u7247<\/strong><\/h2>\n<p>image_path = &#39;path\/to\/image.jpg&#39;<\/p>\n<p>image = Image.open(image_path)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528OpenCV\u8fdb\u884c\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528OpenCV\u8fdb\u884c\u56fe\u50cf\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u7247<\/strong><\/h2>\n<p>image_path = &#39;path\/to\/image.jpg&#39;<\/p>\n<p>image = cv2.imread(image_path)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u7247<\/strong><\/h2>\n<p>cv2.imshow(&#39;Image&#39;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u7b49\u5f85\u56fe\u7247\u52a0\u8f7d<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408Pillow\u548cOpenCV\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u56fe\u7247\u52a0\u8f7d\u7684\u7b49\u5f85\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from PIL import Image<\/p>\n<p>import cv2<\/p>\n<p>def wait_for_image(image_path, timeout=10):<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            # \u5c1d\u8bd5\u52a0\u8f7d\u56fe\u7247<\/p>\n<p>            image = Image.open(image_path)<\/p>\n<p>            print(&#39;Image loaded successfully&#39;)<\/p>\n<p>            break<\/p>\n<p>        except FileNotFoundError:<\/p>\n<p>            # \u5982\u679c\u56fe\u7247\u672a\u627e\u5230\uff0c\u5219\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u91cd\u8bd5<\/p>\n<p>            if time.time() - start_time &gt; timeout:<\/p>\n<p>                print(&#39;Timeout waiting for image&#39;)<\/p>\n<p>                break<\/p>\n<p>            time.sleep(1)<\/p>\n<h2><strong>\u7b49\u5f85\u56fe\u7247\u52a0\u8f7d<\/strong><\/h2>\n<p>image_path = &#39;path\/to\/image.jpg&#39;<\/p>\n<p>wait_for_image(image_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>wait_for_image<\/code>\u51fd\u6570\u4f1a\u5728\u6307\u5b9a\u7684\u8d85\u65f6\u65f6\u95f4\u5185\u4e0d\u65ad\u5c1d\u8bd5\u52a0\u8f7d\u56fe\u7247\uff0c\u76f4\u5230\u56fe\u7247\u52a0\u8f7d\u6210\u529f\u6216\u8d85\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u7b49\u5f85\u56fe\u7247\u51fa\u73b0<\/p>\n<p>\u9664\u4e86Selenium\u3001Pillow\u548cOpenCV\u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u7b49\u5f85\u56fe\u7247\u51fa\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528<code>requests<\/code>\u5e93\u8fdb\u884c\u56fe\u7247\u4e0b\u8f7d\u3001\u7ed3\u5408\u5176\u4ed6\u7f51\u9875\u6293\u53d6\u5de5\u5177\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528requests\u5e93<\/h3>\n<\/p>\n<p><p><code>requests<\/code>\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u4e0b\u8f7d\u7f51\u9875\u548c\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>requests<\/code>\u5e93\u7b49\u5f85\u56fe\u7247\u51fa\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import requests<\/p>\n<p>def wait_for_image(url, timeout=10):<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            # \u5c1d\u8bd5\u4e0b\u8f7d\u56fe\u7247<\/p>\n<p>            response = requests.get(url)<\/p>\n<p>            if response.status_code == 200:<\/p>\n<p>                print(&#39;Image downloaded successfully&#39;)<\/p>\n<p>                break<\/p>\n<p>        except requests.RequestException:<\/p>\n<p>            # \u5982\u679c\u4e0b\u8f7d\u5931\u8d25\uff0c\u5219\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u91cd\u8bd5<\/p>\n<p>            if time.time() - start_time &gt; timeout:<\/p>\n<p>                print(&#39;Timeout waiting for image&#39;)<\/p>\n<p>                break<\/p>\n<p>            time.sleep(1)<\/p>\n<h2><strong>\u7b49\u5f85\u56fe\u7247\u4e0b\u8f7d<\/strong><\/h2>\n<p>image_url = &#39;https:\/\/example.com\/image.jpg&#39;<\/p>\n<p>wait_for_image(image_url)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7ed3\u5408\u5176\u4ed6\u7f51\u9875\u6293\u53d6\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Selenium\u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u7f51\u9875\u6293\u53d6\u5de5\u5177\u53ef\u4ee5\u7528\u6765\u7b49\u5f85\u56fe\u7247\u51fa\u73b0\uff0c\u4f8b\u5982<code>BeautifulSoup<\/code>\u3001<code>Scrapy<\/code>\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u7ed3\u5408<code>BeautifulSoup<\/code>\u7b49\u5f85\u56fe\u7247\u51fa\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>def wait_for_image(url, image_selector, timeout=10):<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            # \u4e0b\u8f7d\u7f51\u9875\u5185\u5bb9<\/p>\n<p>            response = requests.get(url)<\/p>\n<p>            if response.status_code == 200:<\/p>\n<p>                # \u89e3\u6790\u7f51\u9875\u5185\u5bb9<\/p>\n<p>                soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<p>                # \u67e5\u627e\u56fe\u7247\u5143\u7d20<\/p>\n<p>                image_element = soup.select_one(image_selector)<\/p>\n<p>                if image_element and &#39;src&#39; in image_element.attrs:<\/p>\n<p>                    print(&#39;Image found successfully&#39;)<\/p>\n<p>                    break<\/p>\n<p>        except requests.RequestException:<\/p>\n<p>            # \u5982\u679c\u4e0b\u8f7d\u5931\u8d25\uff0c\u5219\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u540e\u91cd\u8bd5<\/p>\n<p>            if time.time() - start_time &gt; timeout:<\/p>\n<p>                print(&#39;Timeout waiting for image&#39;)<\/p>\n<p>                break<\/p>\n<p>            time.sleep(1)<\/p>\n<h2><strong>\u7b49\u5f85\u56fe\u7247\u51fa\u73b0<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>image_selector = &#39;img[src=&quot;image.jpg&quot;]&#39;<\/p>\n<p>wait_for_image(url, image_selector)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<p>\u7b49\u5f85\u56fe\u7247\u51fa\u73b0\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002<strong>\u4f7f\u7528Selenium\u3001Pillow\u3001OpenCV\u7b49\u5de5\u5177\u53ef\u4ee5\u6709\u6548\u5730\u7b49\u5f85\u56fe\u7247\u52a0\u8f7d\u5b8c\u6210<\/strong>\u3002\u7ed3\u5408\u663e\u5f0f\u7b49\u5f85\u3001\u56fe\u7247\u4e0b\u8f7d\u3001\u7f51\u9875\u89e3\u6790\u7b49\u6280\u672f\uff0c\u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u56fe\u7247\u7b49\u5f85\u4efb\u52a1\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u9760\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528Selenium\u7b49\u5f85\u56fe\u7247\u52a0\u8f7d\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Selenium\u8fdb\u884c\u7f51\u9875\u81ea\u52a8\u5316\u65f6\uff0c\u53ef\u4ee5\u5229\u7528WebDriverWait\u7c7b\u6765\u7b49\u5f85\u56fe\u7247\u7684\u51fa\u73b0\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u4f7f\u7528<code>EC.presence_of_element_located<\/code>\u6765\u5b9a\u4f4d\u56fe\u7247\u5143\u7d20\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.support import expected_conditions as EC\n\ndriver = webdriver.Chrome()\ndriver.get(&quot;\u4f60\u7684\u7f51\u9875\u5730\u5740&quot;)\n# \u7b49\u5f85\u56fe\u7247\u52a0\u8f7d\nimage = WebDriverWait(driver, 10).until(\n    EC.presence_of_element_located((By.XPATH, &#39;\u4f60\u7684\u56fe\u7247XPath&#39;))\n)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u64cd\u4f5c\u4e4b\u524d\uff0c\u56fe\u7247\u5df2\u7ecf\u52a0\u8f7d\u5b8c\u6210\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Pillow\u5e93\u68c0\u67e5\u56fe\u7247\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>Pillow\u5e93\u63d0\u4f9b\u4e86\u5904\u7406\u56fe\u7247\u7684\u529f\u80fd\u3002\u5982\u679c\u4f60\u60f3\u68c0\u67e5\u672c\u5730\u6587\u4ef6\u7cfb\u7edf\u4e2d\u67d0\u4e2a\u56fe\u7247\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u65b9\u6cd5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\nimport os\n\nimage_path = &#39;\u4f60\u7684\u56fe\u7247\u8def\u5f84.jpg&#39;\nif os.path.exists(image_path):\n    image = Image.open(image_path)\n    # \u56fe\u7247\u5b58\u5728\uff0c\u53ef\u4ee5\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\nelse:\n    print(&quot;\u56fe\u7247\u4e0d\u5b58\u5728\uff01&quot;)\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5e2e\u52a9\u4f60\u5224\u65ad\u56fe\u7247\u6587\u4ef6\u7684\u53ef\u7528\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528OpenCV\u7b49\u5f85\u56fe\u7247\u7684\u663e\u793a\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5728\u4f7f\u7528OpenCV\u5904\u7406\u56fe\u50cf\uff0c\u53ef\u4ee5\u901a\u8fc7<code>cv2.waitKey()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u7b49\u5f85\u663e\u793a\u56fe\u7247\u3002\u6b64\u51fd\u6570\u4f1a\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u5185\u7b49\u5f85\u6309\u952e\u8f93\u5165\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\n\nimage = cv2.imread(&#39;\u4f60\u7684\u56fe\u7247\u8def\u5f84.jpg&#39;)\ncv2.imshow(&#39;Image&#39;, image)\n# \u7b49\u5f855000\u6beb\u79d2\uff085\u79d2\uff09\u540e\u5173\u95ed\u7a97\u53e3\ncv2.waitKey(5000)\ncv2.destroyAllWindows()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u63a7\u5236\u56fe\u7247\u7684\u5c55\u793a\u65f6\u95f4\uff0c\u540c\u65f6\u4e5f\u80fd\u5728\u663e\u793a\u56fe\u7247\u540e\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7b49\u5f85\u56fe\u7247\u51fa\u73b0\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982\u5229\u7528Selenium\u3001Pillow\u3001OpenCV\u7b49\u5de5\u5177\u3002 \u6700\u63a8\u8350\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1160944,"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\/1160943"}],"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=1160943"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160943\/revisions"}],"predecessor-version":[{"id":1160946,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160943\/revisions\/1160946"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1160944"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1160943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1160943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1160943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}