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)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值