使用pytesseract 和 Pillow操作验证码
(只能操作简单的验证码)
- 首先需要导入响应的模块
pip install pytesseract
pip install Pillow
- 进行验证码的分析
import pytesseract
from selenium import webdriver
import time
from PIL import Image
1. 截取整个页面
2. 根据验证码的id 或者 class 等属性找到 该验证码的位置(获取到左上顶点和右下顶点的位置)
3. 使用坐标对该验证码进行抠图(使用Pillow这个库)
4. 对截取出来的验证码,使用pytesseract库来进行转换
driver = webdriver.Chrome()
driver.get('http://127.0.0.1:8080/image_test')
driver.maximize_window()
t = time.time()
img_name = str(t) + '.png'
driver.save_screenshot("")
yzm = driver.find_element_by_id("image_yzm")
print(yzm.location)
left = yzm.location['x']
top= yzm.location['y']
right = yzm.location['width'] + left
height = yzm.location['height'] + top
im = Image.open(img_name)
img = im.crop((left,top,right,height))
t = time.time()
img_name2 = str(t) + '.png'
img.save(img_name2)
image1 = Image.open(img_name2)
str_image = pytesseract.image_to_string(image1)
print(str_image)