Python3实现的简单验证码识别功能示例


Posted in Python onMay 02, 2018

本文实例讲述了Python3实现的简单验证码识别功能。分享给大家供大家参考,具体如下:

这次的需求是自动登录某机构网站, 其验证码很具特色, 很适合做验证码识别入门demo, 先贴主要代码, 其中图片对比使用了编辑距离算法, 脚本使用了pillow库

from PIL import Image
import requests
import re
splitter = re.compile(r'\d{30}') # 分割二值化后的图片
# distance('11110000', '00000000')
# 比较两个字符串有多少位不同, 返回不同的位数
def distance(string1, string2):
  d_str1 = len(string1)
  d_str2 = len(string2)
  d_arr = [[0] * d_str2 for i in range(d_str1)]
  for i in range(d_str1):
    for j in range(d_str2):
      if string1[i] == string2[j]:
        if i == 0 and j == 0:
          d_arr[i][j] = 0
        elif i != 0 and j == 0:
          d_arr[i][j] = d_arr[i - 1][j]
        elif i == 0 and j != 0:
          d_arr[i][j] = d_arr[i][j - 1]
        else:
          d_arr[i][j] = d_arr[i - 1][j - 1]
      else:
        if i == 0 and j == 0:
          d_arr[i][j] = 1
        elif i != 0 and j == 0:
          d_arr[i][j] = d_arr[i - 1][j] + 1
        elif i == 0 and j != 0:
          d_arr[i][j] = d_arr[i][j - 1] + 1
        else:
          d_arr[i][j] = min(d_arr[i][j - 1], d_arr[i - 1][j], d_arr[i - 1][j - 1]) + 1
  current = max(d_arr[d_str1 - 1][d_str2 - 1], abs(d_str2 - d_str1))
  # print("Levenshtein Distance is",current)
  # print(current)
  return current
# 去除字符串里面连续的1
def no_one(string):
  n_arr = splitter.findall(string)
  n_arr = filter(lambda each_str: each_str != '111111111111111111111111111111', n_arr)
  n_result = ''
  for n_each in n_arr:
    n_result += str(n_each)
  return n_result
opener = requests.session()
res = opener.get('http://60.211.254.236:8402/Ajax/ValidCodeImg.ashx').content
with open('verify.gif', 'wb') as v:
  v.write(res)
img = Image.open('verify.gif')
img = img.convert('L')
size = img.size
# img = img.point(table, '1')
img_arr = img.load()
# for x in range(size[0]):
#   for y in range(size[1]):
#     if img_arr[x, y] > 210:
#       img_arr[x, y] = 1
#     else:
#       img_arr[x, y] = 0
# img.save('after.gif')
inc = 0
str1 = ''
str2 = ''
str3 = ''
cur_str = ''
for x in range(size[0]):
  for y in range(size[1]):
    if img_arr[x, y] > 210:
      cur_str += '1'
    else:
      cur_str += '0'
    # print(img_arr[i, j], end='')
    # cur_str += str(img_arr[x, y])
  inc += 1
  # if inc % 18 == 0:
  #   print('\n----')
  # else:
  #   print('')
  if inc == 18:
    str1 = cur_str
    cur_str = ''
  elif inc == 36:
    str2 = cur_str
    cur_str = ''
  elif inc == 54:
    str3 = cur_str
    cur_str = ''
str1 = str1[:-60]
str2 = str2[:-60]
str3 = str3[:-60]
str1 = no_one(str1)
str2 = no_one(str2)
str3 = no_one(str3)
str1 = str1.strip('1')
str2 = str2.strip('1')
str3 = str3.strip('1')
# print(str1)
# print(str3)
with open('./dict/plus') as plus:
  with open('./dict/minus') as minus:
    p = plus.read()
    m = minus.read()
    is_add = 1 if distance(p, str2) < distance(m, str2) else 0
arr1 = []
arr3 = []
for each in range(1, 10):
  with open('./dict/{}'.format(each)) as f:
    ff = f.read()
    arr1.append([each, distance(ff, str1)])
    arr3.append([each, distance(ff, str3)])
arr1 = sorted(arr1, key=lambda item: item[1])
arr3 = sorted(arr3, key=lambda item: item[1])
result = arr1[0][0] + arr3[0][0] if is_add else arr1[0][0] - arr3[0][0]
print(result)
# login_url = 'http://60.211.254.236:8402/Ajax/Login.ashx?Method=G3_Login'
# login_data = {
#   'loginname': usn,
#   'password': pwd,
#   'validcode': result,
#
# }
# opener.get(login_url, login_data)

字库已经部署到GitHub地址:https://github.com/hldh214/validCode/

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python 基础学习第二弹 类属性和实例属性
Aug 27 Python
Python数组条件过滤filter函数使用示例
Jul 22 Python
Python最基本的数据类型以及对元组的介绍
Apr 14 Python
深入学习python的yield和generator
Mar 10 Python
python类中super()和__init__()的区别
Oct 18 Python
Python 中开发pattern的string模板(template) 实例详解
Apr 01 Python
Python3爬楼梯算法示例
Mar 04 Python
详解Python数据可视化编程 - 词云生成并保存(jieba+WordCloud)
Mar 26 Python
Python 模拟生成动态产生验证码图片的方法
Feb 01 Python
完美解决jupyter由于无法import新包的问题
May 26 Python
读取nii或nii.gz文件中的信息即输出图像操作
Jul 01 Python
Django实现翻页的示例代码
May 24 Python
利用Python在一个文件的头部插入数据的实例
May 02 #Python
python在文本开头插入一行的实例
May 02 #Python
Python实现的根据文件名查找数据文件功能示例
May 02 #Python
对Python3中的print函数以及与python2的对比分析
May 02 #Python
python print 按逗号或空格分隔的方法
May 02 #Python
Django 跨域请求处理的示例代码
May 02 #Python
python 按照固定长度分割字符串的方法小结
Apr 30 #Python
You might like
php共享内存段示例分享
2014/01/20 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十六)
2014/06/30 PHP
PHP中的命名空间详细介绍
2015/07/02 PHP
自制PHP框架之路由与控制器
2017/05/07 PHP
详解在YII2框架中使用UEditor编辑器发布文章
2018/11/02 PHP
php判断电子邮件是否正确方法
2018/12/04 PHP
javascript for循环设法提高性能
2010/02/24 Javascript
基于jquery的鼠标拖动效果代码
2012/05/30 Javascript
jquery post方式传递多个参数值后台以数组的方式进行接收
2013/01/11 Javascript
JavaScript prototype 使用介绍
2013/08/29 Javascript
extJS中常用的4种Ajax异步提交方式
2014/03/07 Javascript
详细介绍jQuery.outerWidth() 函数具体用法
2015/07/20 Javascript
jQuery实现百叶窗焦点图动画效果代码分享(附源码下载)
2016/03/14 Javascript
RequireJS 依赖关系的实例(推荐)
2017/01/21 Javascript
使用webpack搭建react开发环境的方法
2018/05/15 Javascript
浅谈如何使用webpack构建多页面应用
2018/05/30 Javascript
bootstrap里bootstrap动态加载下拉框的实例讲解
2018/08/10 Javascript
微信小程序中this.data与this.setData的区别详解
2018/09/17 Javascript
Vue中的组件及路由使用实例代码详解
2019/05/22 Javascript
vue如何实现自定义底部菜单栏
2019/07/01 Javascript
基于JS实现计算24点算法代码实例解析
2020/07/23 Javascript
基于Vue全局组件与局部组件的区别说明
2020/08/11 Javascript
Python中你应该知道的一些内置函数
2017/03/31 Python
Python中使用双下划线防止类属性被覆盖问题
2019/06/27 Python
Python + opencv对拍照得到的图片进行背景去除的实现方法
2020/11/18 Python
PyCharm2020.3.2安装超详细教程
2021/02/08 Python
html5 Canvas实现图片旋转的示例
2018/01/15 HTML / CSS
英国最大的百货公司:Harrods
2016/08/18 全球购物
乌克兰在线商店的价格比较:Price.ua
2019/07/26 全球购物
党员培训思想汇报
2014/01/07 职场文书
修理厂厂长岗位职责
2014/01/30 职场文书
社区平安建设方案
2014/05/25 职场文书
人事代理委托书
2014/09/27 职场文书
预备党员群众路线教育实践活动思想汇报2014
2014/10/25 职场文书
本溪关门山导游词
2015/02/09 职场文书
iSCSI服务器CHAP双向认证配置
2022/04/01 Servers