Selenium+Tesseract cracking login verification code

preface The type of login verification code I cracked is the type of mathematical formula, i.e... does it look simple, b...

preface

The type of login verification code I cracked is the type of mathematical formula, i.e... does it look simple, but Tesseract's recognition rate of this kind of picture is also very low. If I want to improve the recognition rate in the later stage, I think one is to reduce the noise and sharpen the picture, and the other is to train Tesseract in advance. I recommend jtesboxeditor as the training tool. You can learn about it if you are interested.

1. Install Tesseract

1.1 download Tesseract. Address: Windows Installer made with MinGW-w64 , download and install directly. After the installation, configure the environment variables. Edit the path in the system variable and add the following environment variables.

C:\Program Files(x86)\Tesseract-OCR

After the installation is complete, enter in the cmd command line window:

tesseract -v

The following appears to prove that the installation was successful.

1.2 install pyteseract

pip install pytesseract

2 identification code

2.1 identification logic. First, use selenium to find the verification code label in the browser, then save the screenshot, and then use tesseract to identify the image. If the recognition fails, use selenium to click on the verification code picture, regenerate a verification code, and repeat the previous recognition logic. If the recognition is successful, use eval to calculate the value of the formula, fill in and click the login button.

2.2 code

import time import pytesseract from selenium.webdriver import Chrome from PIL import Image driver = Chrome(executable_path=r'D:\Chrome Downloads\chromedriver.exe') # Storage path of chrome driver url = "http://60.210.111.130:8002/Login.aspx" print("Start crawling, url by{}".format(url)) driver.get(url=url) time.sleep(2) # Find the input tag of username and fill in the username username = driver.find_element_by_id('txtUserName') username.clear() username.send_keys('your username') # Find the input tag of password and fill in the password. If the password is cleared when you click the verification code again, the code should be placed in the while loop password = driver.find_element_by_id('pwd') password.clear() password.send_keys('your password') # 20 attempts times = 0 while True: try: element = driver.find_element_by_id('validimg') element.click() # The picture of the verification code requested each time is different, so you can only take a screenshot and save it locally. driver.save_screenshot('validImage.png') # In windows system, pay attention to page scaling. If it is 150%, then each item is * 1.5. linux system does not need left = element.location['x']*1.5 top = element.location['y']*1.5 right = element.location['x']*1.5 + element.size['width']*1.5 bottom = element.location['y']*1.5 + element.size['height']*1.5 im = Image.open('validImage.png') im = im.crop((left, top, right, bottom)) # Matting im.save('validImage.png') data = pytesseract.image_to_string(Image.open('validImage.png')) if len(data) < 3: # Unrecognized continue if len(data) == 4: # Identification band = number, remove = number data = data[:3] if data[1] == ".": # -No. is often identified as. And replaced with - No data[1] = "-" print("ocr Identification results", data) valid_code = driver.find_element_by_id('validcode') valid_code.clear() valid_code.send_keys(eval(data)) time.sleep(1) btnLogin = driver.find_element_by_id('btnLogin') btnLogin.click() # Get cookie s login_cookie = driver.get_cookie('ASP.NET_SessionId') total_cookie = 'ASP.NET_SessionId={}; autoLogin=null; user=null; pwd=null'.format(login_cookie['value']) print("total_cookie,{}".format(total_cookie)) break except Exception as e: time.sleep(2) times += 1 print("Failed to crack the verification code. Try again{}second".format(times)) if times == 20: raise RuntimeError # linux system should pay attention to exit from chrome driver. Otherwise, if executed multiple times, a large number of chrome driver processes will be generated. driver.quit()

The results are as follows:

One of the comparison pits is that the windows system displays scaling. You can see it in the desktop right-click display settings zoom and layout. If the scale is 150%, then in the screenshot, each parameter must be * 1.5, otherwise the desired verification code cannot be cut.

25 June 2020, 00:25 | Views: 6804

Add new comment

For adding a comment, please log in
or create account

0 comments