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

安装依赖包

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

结果演示

​​

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

相关推荐
啥都想学的又啥都不会的研究生14 分钟前
Redis设计与实现-服务器中的数据库
运维·服务器·数据库·redis·笔记·缓存·性能优化
djykkkkkk26 分钟前
ubuntu 和 RV1126 交叉编译Mosqutiio-1.6.9
linux·运维·ubuntu
sqzr31630 分钟前
【长安大学】苹果手机/平板自动连接认证CHD-WIFI脚本(快捷指令)
智能手机·自动化·ipad·苹果手机·快捷指令·ios系统·长安大学
大小科圣1 小时前
Tomcat介绍及部署
运维·服务器
好多知识都想学1 小时前
第二章Linux 命令概述
linux·运维·服务器
wo3258661451 小时前
浪潮英政服务器CS5420H2配置阵列时报错The reguested command has inualid arguments.解决方法
运维·服务器
熊峰峰2 小时前
Linux第0节:Linux环境的搭建
linux·运维·服务器
一点多余.2 小时前
nginx的使用
运维·nginx
鸭梨山大。2 小时前
linux命令-iptables与firewalld 命令详解
linux·运维·网络
半夏知半秋2 小时前
linux下的网络抓包(tcpdump)介绍
linux·运维·服务器·网络·笔记·学习·tcpdump