python ddddocr 进行文字+数字验证码识别
一、python版本
将自动安装符合自己电脑环境的最新 ddddocr。Python 环境需要小于等于 3.9。
pip install ddddocr
二、字母数字验证码
代码如下:
原始图片
import ddddocr ocr = ddddocr.DdddOcr() with open('111.png','rb') as f: img_bytes = f.read() res = ocr.classification(img_bytes) print(res)
执行结果
三、完整登录一个带验证码系统的代码
from selenium import webdriver from helium import * import base64 import time import ddddocr def open_chrome(): options = webdriver.ChromeOptions() options.add_argument('--no-sandbox') # options.add_argument('--proxy-server=socks5://127.0.0.1:1080') options.add_experimental_option('useAutomationExtension', False) options.add_experimental_option('excludeSwitches', ['enable-automation']) driver = webdriver.Chrome(chrome_options=options) driver.maximize_window() set_driver(driver) go_to("http://127.0.0.1:8000/login/") write("admin","账号") write("admin","密码") code_base64 = driver.find_element_by_css_selector("#code + img").get_attribute('src').split("data:image/png;base64,")[-1] img = base64.b64decode(code_base64) # 识别验证码 ocr = ddddocr.DdddOcr(show_ad=False) code = ocr.classification(img) # code = res['pic_str'] write(code, '验证码') time.sleep(3) click(Button('登 录')) driver.get_screenshot_as_file("test.png") time.sleep(5) kill_browser() if __name__ == '__main__': open_chrome()