滑块验证码识别代码分享

平时我们开发爬虫会遇到各种各样的滑动验证码,如下图所示:

滑块验证码识别代码分享_第1张图片滑块验证码识别代码分享_第2张图片

滑块验证码识别代码分享_第3张图片滑块验证码识别代码分享_第4张图片

为了解决这个问题,我写了一个通用的滑块验证码识别代码,主要是分析图片,然后计算出滑块滑动的像素距离。但是像素距离大多数情况下都不会等于滑动距离,所以需要进行转换。

滑动距离的计算我之前写了一个博客,可以点击查看

《抖音滑块验证码滑动距离计算_滑动验证码 验证判断距离还是对齐-CSDN博客》

最后送上缺口的识别代码:


import base64
import requests
import datetime
from io import BytesIO
from PIL import Image

t1 = datetime.datetime.now()

#PIL图片保存为base64编码
def PIL_base64(img, coding='utf-8'):
    img_format = img.format
    if img_format == None:
        img_format = 'JPEG'

    format_str = 'JPEG'
    if 'png' == img_format.lower():
        format_str = 'PNG'
    if 'gif' == img_format.lower():
        format_str = 'gif'

    if img.mode == "P":
        img = img.convert('RGB')
    if img.mode == "RGBA":
        format_str = 'PNG'
        img_format = 'PNG'

    output_buffer = BytesIO()
    # img.save(output_buffer, format=format_str)
    img.save(output_buffer, quality=100, format=format_str)
    byte_data = output_buffer.getvalue()
    base64_str = 'data:image/' + img_format.lower() + ';base64,' + base64.b64encode(byte_data).decode(coding)

    return base64_str

# 加载图片
img1 = Image.open(r'E:\Python\lixin_project\OpenAPI接口测试\test_img\6号模型图片.jpg')
# 图片转base64
img1_base64 = PIL_base64(img1)

# 验证码识别接口
url = "http://localhost:8000/openapi/verify_code_identify/"
data = {
    # 用户的key
    "key":"9sdPsk5Czyj4vhXujuJw",
    # 验证码类型
    "verify_idf_id":"6",
    # 样例图片
    "img_base64":img1_base64,
    # # 中文点选,空间语义类型验证码的文本描述(这里缺省为空字符串)
    # "words":""
}
header = {"Content-Type": "application/json"}

# 发送请求调用接口
response = requests.post(url=url, json=data, headers=header)

# 获取响应数据,识别结果
print(response.text)
print("耗时:", datetime.datetime.now() - t1)

你可能感兴趣的:(验证码识别,Python,python,神经网络,深度学习,人工智能)