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

来源:互联网 发布:招聘php 编辑:程序博客网 时间:2024/06/06 04:55
需要安装Pillow 、pyocr、pytesseractpip install Pillowpip install pyocrpip install pytesseract#coding=utf-8from selenium import webdriverimport osimport MarketingVPLoginimport unittestfrom time import sleepfrom PIL import Image,ImageEnhanceimport pytesseractchrome_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 0