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

安装依赖包

  • 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()

结果演示

​​

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

相关推荐
4032407311 小时前
[Jetson/Ubuntu 22.04] 解决挂载 exFAT 硬盘报错 “unknown filesystem type“ 及只读权限问题的终极指南
linux·运维·ubuntu
零意@11 小时前
debian如何把新编译的内核镜像替换原来的内核
运维·debian·更新内核版本·linux内核版本更新·debian更新内核
Love丶伊卡洛斯12 小时前
Ubuntu 部署 STUN服务端
linux·运维·ubuntu
有毒的教程13 小时前
SaltStack 开源自动化运维工具详细介绍
运维·开源·saltstack
大房身镇、王师傅13 小时前
【VirtualBox】VirtualBox 7.1.6 RockyLinux10 配置增强功能 设置共享目录
运维·服务器·virtualbox·rockylinux10
betazhou13 小时前
rsync使用案例分析
linux·运维·服务器·rsync·同步数据
晚风予卿云月15 小时前
【Linux】自动化构建—make/Makefile
linux·自动化·make/makefile
minglie115 小时前
谷歌浏览器搜索技巧
运维
脑子进水养啥鱼?15 小时前
Linux find 命令
linux·运维
曹天骄16 小时前
Cloudflare Worker 关联域名访问后出现301 / 308
运维·云计算