当前位置: 首页 > article >正文

滑块验证码识别代码分享

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

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

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

《抖音滑块验证码滑动距离计算_滑动验证码 验证判断距离还是对齐-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)


http://www.kler.cn/news/234335.html

相关文章:

  • 力扣236——二叉树的最近公共祖先
  • [2024]常用的pip指令
  • Docker 容器网络:C++ 客户端 — 服务器应用程序。
  • 【北邮鲁鹏老师计算机视觉课程笔记】01 introduction
  • 【服务器部署】Docker环境的安装
  • Linux内核有什么之内存管理子系统有什么——基础篇之struct vm_area_struct(2)
  • Bert与ChatGPT
  • Java多态原理
  • 学习数据结构和算法的第7天
  • 【MySQL】-12 MySQL索引(上篇MySQL索引类型前置-1)
  • 像素、分辨率、公差的概念
  • 相机图像质量研究(11)常见问题总结:光学结构对成像的影响--像差
  • Vue项目创建
  • 【Java】学习笔记:关于java.sql;
  • 基于vue+node.js的校园跳蚤市场系统多商家
  • Python图形用户界面
  • 假期day6
  • OSDI 2023: Conveyor One-Tool-Fits-All Continuous Software Deployment at Meta
  • ###C语言程序设计-----C语言学习(11)#数据的存储和基本数据类型
  • mfc110.dll是什么?解决mfc110.dll丢失windows系统常见问题
  • blender几何节点中样条线参数中的系数(factor)是个什么概念?
  • 2.10日学习打卡----初学RocketMQ(一)
  • Open CASCADE学习|2个TCL命令转C++
  • 【Linux】make和Makefile
  • Tomcat之虚拟主机
  • 基于微信小程序的校园二手交易平台
  • ChatGPT高效提问—prompt常见用法(续篇九)
  • Nginx实战:2-日志配置
  • wireshark抓包问题及学习
  • Uniapp(uni-app)学习与快速上手教程