使用 Python 实现点触验证码识别与处理


目标
本节的目标是使用 Python 语言实现点触验证码的识别与验证,不依赖第三方验证码识别平台。

准备工作
我们将使用 Selenium 库来驱动 Chrome 浏览器,并借助一些图像处理库来进行验证码识别。在此之前,请确保已正确安装了 Selenium 库、Chrome 浏览器,并配置好了 ChromeDriver。

了解点触验证码
点触验证码是一种需要点击图像中指定区域来完成验证的方式。常见的点触验证码样式如下图所示:


识别思路
识别此类验证码的难点主要在于文字和图像的识别。我们将使用 OCR 技术结合图像处理算法,来识别验证码中的文字和目标区域。

实现步骤
1. 安装依赖
首先,安装所需的 Python 库:

sh

pip install selenium pillow pytesseract opencv-python
2. 初始化 WebDriver
python
复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

CHROME_DRIVER_PATH = '/path/to/chromedriver'
URL = 'http://admin.touclick.com/login.html'

def init_driver():
    service = Service(CHROME_DRIVER_PATH)
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(service=service, options=options)
    driver.get(URL)
    return driver
3. 获取验证码图像
python

from PIL import Image
import io

def capture_captcha(driver):
    wait = WebDriverWait(driver, 20)
    captcha_element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "touclick-pub-content")))
    location = captcha_element.location
    size = captcha_element.size
    screenshot = driver.get_screenshot_as_png()
    screenshot = Image.open(io.BytesIO(screenshot))

    left = location['x']
    top = location['y']
    right = location['x'] + size['width']
    bottom = location['y'] + size['height']

    captcha_image = screenshot.crop((left, top, right, bottom))
    captcha_image.save('captcha.png')
    return captcha_image
4. 识别验证码文字和点击
为了进行验证码的识别,我们可以使用 Tesseract OCR 和 OpenCV。以下是一个简单的示例,演示如何使用这些工具:

python

import pytesseract
import cv2
import numpy as np

def recognize_text(image):
    gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    coordinates = []
    for contour in contours:
        x, y, w, h = cv2.boundingRect(contour)
        if w > 15 and h > 15:  # Filter out small contours
            coordinates.append((x + w // 2, y + h // 2))

    return coordinates

def click_captcha(driver, coordinates):
    captcha_element = driver.find_element(By.CLASS_NAME, "touclick-pub-content")
    actions = ActionChains(driver)
    for coordinate in coordinates:
        actions.move_to_element_with_offset(cap更多内容联系1436423940tcha_element, coordinate[0], coordinate[1]).click().perform()
        time.sleep(1)  # pause between clicks

def main():
    driver = init_driver()
    driver.find_element(By.ID, 'email').send_keys('your-email@example.com')
    driver.find_element(By.ID, 'password').send_keys('your-password')

    captcha_image = capture_captcha(driver)
    coordinates = recognize_text(captcha_image)
    click_captcha(driver, coordinates)

    driver.quit()

if __name__ == "__main__":
    main()

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值