Python3自动识别验证码(简单数字识别)转载http://www.cnblogs.com/landhu/p/4968577.html

需要安装Pillow 、pyocr、pytesseract
pip install Pillow
pip install pyocr
pip install pytesseract
#coding=utf-8
from selenium import webdriver
import os
import MarketingVPLogin
import unittest
from time import sleep
from PIL import Image,ImageEnhance
import pytesseract

chrome_driver_path = r"E:\PyCharm(Python)script\MiaoDJ\chromedriver.exe"

class Manager(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        chrome_driver = os.path.abspath(chrome_driver_path)
        os.environ["webdriver.Chrome.driver"] = chrome_driver
        cls.driver = webdriver.Chrome(chrome_driver)
        browser = cls.driver
        browser.maximize_window()
        sleep(3)
        browser.get("http://test.miaodj.cn/index.php/System/Login/login")
        sleep(3)
        browser.find_element_by_id("username").send_keys("admin")
        browser.find_element_by_id("pwd").send_keys("miaodj123456")
        browser.save_screenshot("E:\pythonimage\image01.png")
        imglement = browser.find_element_by_id("code_img")    #定位验证码
        location = imglement.location     #获取验证码X,Y的坐标
        size = imglement.size             #获取验证码的长宽
        rangle = (int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height']))  #写成我们需要的位置坐标
        image = Image.open(("E:\pythonimage\image01.png"))     #打开截图
        frame4 = image.crop(rangle)                #使用image的crop函数,从截图中再次截取我们的区域
        frame4.save("E:\pythonimage\yanzhengma01.png")
        qq = Image.open("E:\pythonimage\yanzhengma01.png")
        text = pytesseract.image_to_string(qq).strip() #  使用image_to_string识别验证码
        print(text)

你可能感兴趣的:(Python3)