1.使用pytesseract模块和PIL模块解决
pytesseract模块和PIL模块可以解决不太复杂的验证码问题。首先需要安装:
pip install pytesseract
pip install pil
解决思路如下:
- 截屏整个屏幕
- 获得验证码坐标数据
- 根据坐标数据抠图
- 使用pytesseract模块进行验证
代码如下:获取当前页面的验证码
import time
from selenium import webdriver
from PIL import Image
import pytesseract
from builtins import str
class TestCase(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('http://localhost:8080/jpress/user/register')
self.driver.maximize_window()
def test1(self):
#获取验证码图片
t = time.time() #获取当前时间
picture_name1 = str(t)+'.png'
self.driver.save_screenshot(picture_name1) #保存截屏
ce = self.driver.find_element_by_id("captchaimg")
print(ce.location)
left = ce.location['x']
top = ce.location['y']
right = ce.size['width'] + left
height = ce.size['height'] + top
im = Image.open(picture_name1)
# 抠图
img = im.crop((left, top, right, height))
t = time.time()
picture_name2 = str(t)+'.png'
img.save(picture_name2)#这里就是截取到的验证码图片
self.driver.close()
image1 = Image.open(picture_name2)
str1 = pytesseract.image_to_string(image1)
print(str1)
if __name__ == '__main__':
case = TestCase()
case.test1()
控制台无输出
2.使用第三方的API来实现
可以第三方的AI库进行识别,我使用万维易源的API来实现,大家如果有其他的网站也行。
首先要下载一个SDK,将其解压放到项目的lib目录下:
然后代码如下所示:my_appId和my_appSecret是购买了其图片验证码识别后,用相关信息进行更换。1621131086.506006.png是在上一个方法中截取出来的
from selenium import webdriver
from PIL import Image
import pytesseract
from builtins import str
from lib.ShowapiRequest import ShowapiRequest
class TestCase(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('http://localhost:8080/jpress/user/register')
self.driver.maximize_window()
def test1(self):
r = ShowapiRequest("http://route.showapi.com/184-1", "my_appId", "my_appSecret")
r.addFilePara("image", "1621131086.506006.png")
r.addBodyPara("typeId", "34")
r.addBodyPara("convert_to_jpg", "0")
res = r.post()
print(res.text)
print(res.json()['showapi_res_body']['Result'])
if __name__ == '__main__':
case = TestCase()
case.test1()
控制台输出: