前言

工作需要然后研究了一下selenium,通过python实现了一遍(python真香)。

因为公司项目是java(-_-),当然java也有selenium的jar包,用法都差不多。但是调用tesseract-ocr是识别验证码没有py那么方便。

tess4J这个jar包对tesseract进行了封装无需安装tesseract-ocr,但是经过测试只能识别正常规整的字体,验证码很多都不是很规整的字体。

解决方案:训练调教学习生成自己的字库
Tesseract训练字库,提高正确识别率
通俗点 当机器识别到这个图形时我们就告诉机器这个就是A...

python实现验证码识别登录

忘了介绍浏览器驱动了。。。
各浏览器下载地址:

下面只是简单案例,仅供参考(散装python)

import re  # 用于正则
from PIL import Image  # 用于打开图片和对图片处理
import pytesseract  # 用于图片转文字
from selenium import webdriver  # 用于打开网站
import time  # 代码运行停顿
from selenium.webdriver.support.select import Select
 
 
class VerificationCode:
    def __init__(self):
        #option = webdriver.ChromeOptions()
        #option.add_argument('headless')  # 设置option
        #self.driver = webdriver.Chrome(options=option)  # 调用带参数的谷歌浏览器
        #可以添加系统环境变量
        self.driver = webdriver.Chrome(executable_path ="D:\\App\\chromedrive\\chromedriver.exe")
        self.find_element = self.driver.find_element_by_css_selector
 


    def get_pictures(self):
        self.driver.get('url敏感')  # 打开登陆页面
        time.sleep(3)
        #首次进入提示框点击确认
        checkboxes = self.driver.find_elements_by_css_selector('a.layui-layer-btn0')
        for checkbox in checkboxes: 
            checkbox.click()
        
        self.driver.find_element_by_id("Login").click() 
        #账号
        self.driver.find_element_by_id("loginId").send_keys("123456")
        #密码
        self.driver.find_element_by_id("userPassword").send_keys("123456")




        self.driver.save_screenshot('D:\\1\\pic.png')  # 全屏截图
        page_snap_obj = Image.open('D:\\1\\pic.png')
        img = self.find_element('#checkCodeImage')  # 验证码元素位置
        time.sleep(1)
        location = img.location
        size = img.size  # 获取验证码的大小参数
        left = location['x']
        top = location['y']

        right = left + size['width']
        bottom = top + size['height']
        
        image_obj = page_snap_obj.crop((left, top, right, bottom))  # 按照验证码的长宽,切割验证码
        #image_obj.show()  # 打开切割后的完整验证码
        #self.driver.close()  # 处理完验证码后关闭浏览器
        return image_obj
 
 #图片处理方法,这个网站的验证码太easy 二值化通过阈值直接可以取到想要的图像
 #复杂的可以通过降噪(领域检测法)处理
    def processing_image(self):
        image_obj = self.get_pictures()  # 获取验证码

        img = image_obj.convert("L")  # 转灰度
        #img.show()
        pixdata = img.load()
        w, h = img.size
        threshold = 100
        # 遍历所有像素,大于阈值的为黑色
        for y in range(h):
            for x in range(w):
                if pixdata[x, y] < threshold:
                    pixdata[x, y] = 0
                else:
                    pixdata[x, y] = 255
        return img

    def image_str(self):
        image = self.processing_image()
        #可以添加系统环境变量
        pytesseract.pytesseract.tesseract_cmd = r"D:\\App\\tesseractOcr\\tesseract.exe"  # 设置pyteseract路径
        result = pytesseract.image_to_string(image)  # 图片转文字
        resultj = re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])", "", result)  # 去除识别出来的特殊字符
        print('验证码'+resultj)
        result_four = resultj[0:5]  # 只获取前4个字符
        # print(resultj)  # 打印识别的验证码
        self.driver.find_element_by_id("checkCode").send_keys(resultj)

		#点击登录
        self.driver.find_element_by_id("qy_submit").click()
        time.sleep(1)
        
        #网站比较特殊 需要二次登录
        #切换到iframe
        self.driver.switch_to_frame("bsryIframe")

        # 定位到下拉框
        select=self.driver.find_element_by_id("smrySelect")
        options_list = select.find_elements_by_tag_name('option')
        # 遍历option
        for option in options_list:
            if "许美琼" in option.text:
                option.click();
        #输入密码
        self.driver.find_element_by_id("zrrPass").send_keys("123456")
        #点击登录
        self.driver.find_element_by_id("submitBtn").click()

        return result_four


 
if __name__ == '__main__':
    a = VerificationCode()
    a.image_str()

就这样吧,下一篇记录一下java的方式(比较麻烦)

Q.E.D.