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

安装依赖包

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

结果演示

​​

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

相关推荐
背锅攻城师9 分钟前
linux————根据端口查找运行目录的三种方法
linux·运维·服务器
暖阳与晚风11 分钟前
性能测试-nmon性能监控工具(八)
linux·运维·服务器
梓沂1 小时前
nginx过滤爬虫访问
运维·爬虫·nginx
最新小梦3 小时前
Docker Compose与私有仓库部署
运维·docker·容器
司职在下3 小时前
828华为云征文|Flexus云服务器X实例快速部署在线测评平台,适用各种信息学教学
运维·服务器·华为云
超维机器人3 小时前
智能巡检机器人局部放电检测的应用
运维·人工智能·算法·机器人
weixin_520475743 小时前
nginx部署时的路径配置问题
运维·nginx
阿瑾06184 小时前
【Linux】进程间通信——System V共享内存
linux·运维·服务器
leSerein_4 小时前
【Docker】docker的一些常用命令
linux·运维·docker·容器