当前位置:  开发笔记 > 编程语言 > 正文

python入门之识别验证码介绍

这篇文章主要介绍了python中识别验证码的相关资料,这属于学习python的基本入门教程,文中介绍的非常详细,文末也给出了完整的示例代码,需要的朋友们可以参考学习,下面来一起看看吧。
前言

验证码?我也能破解?

关于验证码的介绍就不多说了,各种各样的验证码在人们生活中时不时就会冒出来,身为学生日常接触最多的就是教务处系统的验证码了,比如如下的验证码:

比如数字六的矩阵

num_6=[
0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,1,1,0,0,0,0,0,0,
0,0,0,0,1,1,1,0,0,0,0,0,0,
0,0,0,1,1,1,0,0,0,0,0,0,0,
0,0,0,1,1,0,0,0,0,0,0,0,0,
0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,1,1,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,0,0,0,0,
0,1,1,0,0,0,0,1,1,1,0,0,0,
0,1,1,0,0,0,0,0,1,1,0,0,0,
0,1,1,0,0,0,0,0,1,1,0,0,0,
0,1,1,1,0,0,0,1,1,1,0,0,0,
0,0,1,1,1,1,1,1,1,0,0,0,0,
0,0,0,1,1,1,1,1,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,
]

远远望过去,眯着眼睛还是能分辨出来的。

因为验证码十分规整,每个数字所在的位置都是固定的,所以并不需要涉及什么机器学习的算法,只是简单的进行一下矩阵的比对就可以了,在所有的实现做好的矩阵中找到相似度最高的矩阵就可以了,在这里的比对方法多种多样,反正数据简单能正确识别出来就好。

至此,咱们的验证码识别工作就结束了。

这次进行的验证码识别主要采用python的PIL进行图片操作,模拟登陆自动填写验证码的全部代码请看这里:

示例代码

# -*- coding: utf-8 -*
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import re
import requests
import io
import os
import json
from PIL import Image
from PIL import ImageEnhance
from bs4 import BeautifulSoup

import mdata

class Student:
 def __init__(self, user,password):
  self.user = str(user)
  self.password = str(password)
  self.s = requests.Session()

 def login(self):
  url = "http://202.118.31.197/ACTIONLOGON.APPPROCESS?mode=4"
  res = self.s.get(url).text
  imageUrl = 'http://202.118.31.197/'+re.findall('his):
     his=r
     chrr=i
   verifyCode+=chrr
   # print "辅助输入验证码完毕:",verifyCode
  data= {
  'WebUserNO':str(self.user),
  'Password':str(self.password),
  'Agnomen':verifyCode,
  }
  url = "http://202.118.31.197/ACTIONLOGON.APPPROCESS?mode=4"
  t = self.s.post(url,data=data).text
  if re.findall("images/Logout2",t)==[]:
   l = '[0,"'+re.findall('alert((.+?));',t)[1][1][2:-2]+'"]'+" "+self.user+" "+self.password+"\n"
   # print l
   # return '[0,"'+re.findall('alert((.+?));',t)[1][1][2:-2]+'"]'
   return [False,l]
  else:
   l = '登录成功 '+re.findall('! (.+?) ',t)[0]+" "+self.user+" "+self.password+"\n"
   # print l
   return [True,l]

 def getInfo(self):
  imageUrl = 'http://202.118.31.197/ACTIONDSPUSERPHOTO.APPPROCESS'
  data = self.s.get('http://202.118.31.197/ACTIONQUERYBASESTUDENTINFO.APPPROCESS?mode=3').text #学籍信息
  data = BeautifulSoup(data,"lxml")
  q = data.find_all("table",attrs={'align':"left"})
  a = []
  for i in q[0]:
   if type(i)==type(q[0]) :
    for j in i :
     if type(j) ==type(i):
      a.append(j.text)
  for i in q[1]:
   if type(i)==type(q[1]) :
    for j in i :
     if type(j) ==type(i):
      a.append(j.text)
  data = {}
  for i in range(1,len(a),2):
   data[a[i-1]]=a[i]
  # data['照片'] = io.BytesIO(self.s.get(imageUrl).content)
  return json.dumps(data)

 def getPic(self):
  imageUrl = 'http://202.118.31.197/ACTIONDSPUSERPHOTO.APPPROCESS'
  pic = Image.open(io.BytesIO(self.s.get(imageUrl).content))
  return pic

 def getScore(self):
   score = self.s.get('http://202.118.31.197/ACTIONQUERYSTUDENTSCORE.APPPROCESS').text #成绩单
   score = BeautifulSoup(score, "lxml")
   q = score.find_all(attrs={'height':"36"})[0]
   point = q.text
   print point[point.find('平均学分绩点'):]
   table = score.html.body.table
   people = table.find_all(attrs={'height' : '36'})[0].string
   r = table.find_all('table',attrs={'align' : 'left'})[0].find_all('tr')
   subject = []
   lesson = []
   for i in r[0]:
    if type(r[0])==type(i):
     subject.append(i.string)
   for i in r:
    k=0
    temp = {}
    for j in i:
     if type(r[0])==type(j):
      temp[subject[k]] = j.string
      k+=1
    lesson.append(temp)
   lesson.pop()
   lesson.pop(0)
   return json.dumps(lesson)

 def logoff(self):
  return self.s.get('http://202.118.31.197/ACTIONLOGOUT.APPPROCESS').text

if __name__ == "__main__":
 a = Student(20150000,20150000)
 r = a.login()
 print r[1]
 if r[0]:
  r = json.loads(a.getScore())
  for i in r:
   for j in i:
    print i[j],
   print
  q = json.loads(a.getInfo())
  for i in q:
   print i,q[i]
  a.getPic().show()
 a.logoff()

更多python入门之识别验证码介绍相关文章请关注PHP中文网!

推荐阅读
mobiledu2402852413
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有