自动化脚本-图片验证码识别登陆

安装依赖包

  • pip install requests

    requests 模块是 Python 中一个常用的 HTTP 库,用于发送 HTTP 请求和处理 HTTP 响应。它提供了简洁而友好的 API,使得在 Python 中进行 HTTP 请求变得十分方便(本文用于进行验证码下载)

  • pip install pillow

    "Pillow" 是一个 Python 的图像处理库,是 "Python Imaging Library (PIL)" 库的一个分支。它提供了大量的图像处理方法,可以处理的图片格式包括 BMP、EPS、GIF、IM、JPEG、MSP、PCX、PNG、PPM、PDF、SPIDER、TIFF、WebP 等图片格式。(本文用于图片读取)

  • pip install pytesseract

    pytesseract用于与 Tesseract OCR 引擎进行交互的 Python 包。在安装 pytesseract 之前,你需要确保已经在系统上安装了 Tesseract OCR 引擎,因为 pytesseract 只是一个 Python 的封装器,依赖于 Tesseract, Tesseract 引擎可以来从图像中提取文本信息(若使用下文中ddddocr识别则无需安装)

  • 安装 Tesseract

参照上文Tesseract安装(经实践识别准确率较低,若使用下文中ddddocr识别则无需安装)

  • pip install opencv-python

    opencv-python包含了一系列用于图像处理、计算机视觉和机器学习的功能,用于降噪、二值化和灰度处理,提高识别准确率

灰度处理效果如下图:

  • pip install ddddocr

    ddddocr开源库,验证码识别率高,使用简单方便,实现了多种验证码识别的方式,github项目地址:https://github.com/sml2h3/ddddocr

代码实现

python 复制代码
import cv2
# import pytesseract
import numpy as np
import ddddocr
from PIL import Image
from selenium import webdriver
import requests
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

# 创建浏览器对象
options = Options()
options.add_experimental_option("detach", True)
# 禁用证书验证
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(options=options)
driver.get("访问网址")

# 输入用户名和密码
username = driver.find_element(By.XPATH, "//input[@name='pwd_username']")
password = driver.find_element(By.XPATH, "//input[@name='pwd_pwd']")
username.send_keys("用户名")
password.send_keys("密码")

# 获取验证码图片链接
div_element = driver.find_element(By.CLASS_NAME,'capimg')
img_element = div_element.find_element(By.XPATH, './/img')
img_url = img_element.get_attribute("src")

# 下载验证码图片
img_response = requests.get(img_url, verify=False)
with open("captcha.jpg", "wb") as f:
    f.write(img_response.content)
    
# ddddocr 图像文字识别,正确率高
ocr = ddddocr.DdddOcr()

# 图像预处理:灰度化、二值化
captcha_image = cv2.imread("captcha.jpg")
# 灰度
gray_image = cv2.cvtColor(captcha_image, cv2.COLOR_BGR2GRAY)
# OTSU阈值二值化
_, binary_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

# 保存预处理后的图片(可选)
cv2.imwrite('processed_captcha.jpg', binary_image)

# 使用 PIL 打开预处理后的图片
captcha_pil = Image.open('processed_captcha.jpg')

# 使用 pytesseract 进行 OCR 识别 【识别正确率低】
# captcha_text = pytesseract.image_to_string(captcha_pil)

# 使用 ddddocr 识别,正确率高
captcha_text = ocr.classification(captcha_pil)

# 输入验证码
captcha_input = driver.find_element(By.XPATH, "//input[@name='pwd_captcah']")
captcha_input.send_keys(captcha_text)

# 提交登录
login_button = driver.find_element(By.CLASS_NAME, 'btn-login')
login_button.click()

# 关闭浏览器
# driver.quit()

结果演示

​​

自动化脚本-图片验证码识别登陆

相关推荐
lolo大魔王4 小时前
Linux 数据文件处理实战:排序、搜索、压缩、归档一站式详解
linux·运维·服务器
llrraa20104 小时前
配置docker国内镜像源
运维·docker·容器
starvapour4 小时前
Ubuntu切换到Fcitx5中文输入法
linux·运维·ubuntu
裴东青4 小时前
08-实战:RuoYi-Vue项目的自动化发布
ci/cd·自动化
xhbh6665 小时前
网关端口映射和路由器端口转发有什么区别?配置要点全解析
运维·服务器·网络·智能路由器·端口映射·映射·无痕网关
STDD5 小时前
Soulmask《灵魂面具》 专用服务器搭建教程
运维·服务器·github
lolo大魔王5 小时前
Linux的监测程序
linux·运维·github
半壶清水5 小时前
用P4 Tutorial、BMv2 和 Mininet‌解析网络第一集------模拟环境搭建
运维·服务器·网络·网络协议·tcp/ip
.YYY5 小时前
RHCE--Linux循环执行的例行性任务:crontab从入门到精通
linux·运维·服务器
木欣欣粉皮5 小时前
解决Ubuntu 26.04的挂起状态唤醒问题
linux·运维·ubuntu