一、问题背景:
某些网站和平台在点击操作过程中,经常会出现一些奇怪的验证码。这些验证码手动操作能够解决,但是出现在自动化业务流程里面将会造成不小的困难。比如涉及到滑块遮挡物的验证、双螺旋的验证、文字识别匹配图片位置点选等。目前可以借助外部接口的方式获取偏移的位置或坐标等,实现部分复杂验证码的处理。
本次涉及验证码接口的网站,云码:https://www.jfbym.com/test/219.html;图鉴:http://www.ttshitu.com/
二、涉及验证码场景:
(1)滑块遮挡物体验证:需要拖动滑块根据提示内容,呈现出一个或多个内容


(2)滑块双螺旋验证:拖动滑块外螺旋图片旋转,与内圆图片匹配


(3)文字匹配点选验证:通过提示内容,依次点选图片内容

(4)复杂模糊验证码图片验证



三、具体代码:
import base64
import requests
import json
from ctypes import windll
from PIL import Image
def get_len(path, token, type):
"""
获取需要识别物体与初始位置的距离
滑块遮挡物体type:20226
滑块双螺旋type:90003
文字匹配点选type:30009
"""
url = "http://api.jfbym.com/api/YmServer/customApi"
with open(path,'rb') as f:
im = base64.b64encode(f.read()).decode()
data = {
'token': token, #输入自己的token
'type': type,
'image':im,
}
_headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=_headers, json=data)
result = response.json()
return result
def get_ppi():
"""
获取屏幕的缩放
"""
LOGPIXELSX = 88
LOGPIXELSY = 90
user32 = windll.user32
user32.SetProcessDPIAware()
dc = user32.GetDC(0)
pix_per_inch = windll.gdi32.GetDeviceCaps(dc, LOGPIXELSX)
user32.ReleaseDC(0, dc)
res = float.as_integer_ratio(round(pix_per_inch / 96,2))
return res[1]/res[0]
def resize_image(input_image_path, output_image_path, target_size):
"""
调整图片格式大小
target_size:需要设置成640
"""
image = Image.open(input_image_path)
# 计算调整后的宽度和高度
width, height = image.size
target_size = int(target_size)
if width > height:
new_width = target_size
new_height = int(height * target_size / width)
else:
new_width = int(width * target_size / height)
new_height = target_size
# 调整图片大小
resized_image = image.resize((new_width, new_height))
# 保存调整后的图片
resized_image.save(output_image_path)
def base64_api(uname, pwd, img, typeid):
"""
图片文字类型(默认 3 数英混合):
1 : 纯数字
1001:纯数字2
2 : 纯英文
1002:纯英文2
3 : 数英混合
1003:数英混合2
4 : 闪动GIF
7 : 无感学习(独家)
11 : 计算题
1005: 快速计算题
16 : 汉字
32 : 通用文字识别(证件、单据)
66: 问答题
"""
with open(img, 'rb') as f:
base64_data = base64.b64encode(f.read())
b64 = base64_data.decode()
data = {"username": uname, "password": pwd, "typeid": typeid, "image": b64}
result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
if result['success']:
return result["data"]["result"]
else:
return result["message"]
return ""(1)滑块遮挡物体验证:

(2)滑块双螺旋验证:

(3)文字匹配点选验证:

(4)复杂模糊验证码图片验证

四、演示操作:
目前测试场景淘宝、天猫、得物小程序等



五、其它验证码:
欢迎各位小伙伴补充交流~~