Python+selenium+ddddocr图片验证码识别

ddddocr是一个用于识别验证码的开源库

  1. 安装ddddocr

pip install ddddocr

2、图片验证码识别的思路是:

先打开登录页面,然后将整个页面截图,通过driver.find_element定位到图片验证码再截取验证码部分,然后使用第3方库ddddocr进行破解,附上完整代码:

import time,ddddocr
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

option = webdriver.ChromeOptions()
option.add_experimental_option('detach',True)
path = Service('chromedriver.exe')
driver = webdriver.Chrome(service=path)

# 窗口最大化
driver.maximize_window()
driver.get('http://116.62.170.243:8011/')
time.sleep(5)
# 截取当前网页
driver.save_screenshot('web_screen.png')
page_snap_obj = Image.open('web_screen.png')
# 定位验证码
img=driver.find_element(By.CLASS_NAME, 'login-code-img')  
time.sleep(1)
location = img.location
size = img.size
left = location['x']
top = location['y']
right = left + size['width']
bottom = top + size['height']
image_obj = page_snap_obj.crop((left, top, right, bottom))
# 保存我们截下来的验证码图片,并读取验证码内容
img_code = image_obj.save('vfi_code.png')
ocr1=ddddocr.DdddOcr()

with open(r'D:\work\vfi_code.png','rb')as f:
    img_bytes=f.read()
res=ocr1.classification(img_bytes)
print('验证码:' + res)

3、验证码图片:

4、执行代码,识别结果

Python+selenium+ddddocr图片验证码识别_第1张图片

你可能感兴趣的:(selenium,selenium)