前言
- 在使用自动化登录网站时,经常输入用户名和密码后就会遇到验证码,今天介绍一款通用验证码识别 OCR库,它的名字是ddddocr。
一、安装
pip install ddddocr
二、使用ddddocr
获取数字/字母验证码
import os
import ddddocr
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
class GetVerificationCode:
def __init__(self):
self.drive = webdriver.Chrome()
self.drive.maximize_window()
self.drive.implicitly_wait(2)
def getverification(self, location):
'''获取验证码信息'''
current_location = os.path.dirname(__file__)
self.drive.save_screenshot(f'{current_location}\\printscreen.png')
position = self.drive.find_element(By.CSS_SELECTOR, location)
location = position.location
size = position.size
rangle = (int(location['x']), int(location['y']),
int(location['x'] + size['width']),
int(location['y'] + size['height']))
i = Image.open(f'{current_location}\\printscreen.png')
fimg = i.crop(rangle)
fimg = fimg.convert('RGB')
fimg.save(f'{current_location}\\code.png')
ocr = ddddocr.DdddOcr()
with open(f'{current_location}\\code.png', 'rb') as f:
img_bytes = f.read()
self.res = ocr.classification(img_bytes)
print('识别出的验证码为:' + self.res)
def login(self):
'''登录'''
self.drive.get('需要获取验证码的网站')
self.getverification('.bgw')
if __name__ == '__main__':
run = GetVerificationCode()
run.login()
import os
import ddddocr
from selenium import webdriver
from selenium.webdriver.common.by import By
class GetVerificationCode:
def __init__(self):
self.drive = webdriver.Chrome()
self.drive.maximize_window()
self.drive.implicitly_wait(2)
def getVerification(self, location):
'''获取验证码信息'''
current_location = os.path.dirname(__file__)
position = self.drive.find_element(By.CSS_SELECTOR, location)
position.screenshot(f'{current_location}\\code.png')
ocr = ddddocr.DdddOcr()
with open(f'{current_location}\\code.png', 'rb') as f:
img_bytes = f.read()
self.res = ocr.classification(img_bytes)
print('识别出的验证码为:' + self.res)
def login(self):
'''登录'''
self.drive.get('需要获取验证码的网站')
self.getVerification('.bgw')
if __name__ == '__main__':
run = GetVerificationCode()
run.login()