Python3实现的简单验证码识别功能示例-创新互联

本文实例讲述了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)

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:Python3实现的简单验证码识别功能示例-创新互联
本文地址:http://csdahua.cn/article/csjcph.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流