1.To identify the login validation code, you need to install Tesseract-OCR at http://jaist.dl.sourceforge.net/project/tesseract-ocr-alt/tesserAct-ocr-setup-3.02.02.exe, the next step is after installation.
2.Precondition:
A==PIL has been installed (3.6 version is Pillow).
B==The computer has been installed Tesseract-OCR.
C==If the tool is Pycharm, the pytesseract package must be introduced in Pycharm setting.
3.The code is as follows:
from selenium import webdriver import time import unittest from PIL import Image from PIL import ImageEnhance import pytesseract driver = webdriver.Ie() driver.get('https://passport.baidu.com/?getpassindex') driver.implicitly_wait(3) driver.maximize_window() driver.save_screenshot('E:\\password.png') #Intercept the current web page, which has the verification code we need.Imagelement = driver.find_element_by_xpath (".//*[@id='forgotsel']/div/div[3]/img")")Location = imagelement.location x gets the X of the verification code, coordinates of the Y axis.Size = imagelement.size, to get the length of the verification code.CoderAnge= (int (location['x']), int (location['y']), int (location['x']+size['width'])).Int (locatioN['y']+size['height'])) we write the position coordinates we need to intercept.I = Image.open ('E:\\password.png')Fram4 = i.crop (c)Oderange) we use Image's crop function to intercept the area we need from the screenshot.Fram4.save ('E:\\fram4.png')I2 = Image.open ('E:\\fram4.)Png')Imgry = i2.convert ('L')Sharpness = ImageEnhance.Contrast (imgry)I3 = sharpness.enhanCE (3) #3.0 is the saturation of image.I3.save ('E:\\image_code.png')I4 = Image.open ('E:\\image_code.png')Text = PYTEsseract.image_to_string (i2).Strip ().Strip uses the image_to_string to identify the verification code.Print (text)
4.After the code was written successfully, the right-click operation was reported and the wrong information :
5.After consulting the information, the solution is as follows: find the pytesseract.py file and modify the contents :
Run again successfully (Note: personal feel that the verification code after OCR is not clear enough)