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

安装依赖包

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

结果演示

​​

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

相关推荐
知白守黑2671 小时前
Ansible角色
运维·服务器·ansible
Jwest20211 小时前
工业显示器在地铁电力监控与运维中的应用
运维·计算机外设
明达智控技术6 小时前
MR30分布式I/O模块在化工行业的应用
物联网·自动化
双翌视觉6 小时前
机器视觉的手机柔性屏贴合应用
智能手机·自动化·视觉检测·机器视觉
Mr. Cao code8 小时前
Docker:颠覆传统虚拟化的轻量级革命
linux·运维·ubuntu·docker·容器
wanhengidc8 小时前
云手机运行流畅,秒开不卡顿
运维·网络·科技·游戏·智能手机
笨小孩@GF 知行合一9 小时前
OSPF实验:外部路由引入
运维·网络·hcip·数通·ospf
asdfg12589639 小时前
为什么要在出口路由器router配置NAT与默认路由
运维·网络·计算机网络
bug攻城狮10 小时前
Skopeo 工具介绍与 CentOS 7 安装指南
linux·运维·centos
宇宙第一小趴菜10 小时前
08 修改自己的Centos的软件源
linux·运维·centos