Python模拟登陆及验证码识别

2025-07-19 03:08:14

1、对验证码的识别使用tesseract模块,识别验证码,代码及演示结果如下。主要是从验证码链接打开验证码图片,调节灰度,识别栓疠瑕愤字符。#image=image.point(lambda x: 0 if x<50 else 255)这是关键!##关乎识别的准确率#url='http://202.116.160.170/CheckCode.aspx 'from urllib.request import urlopenpath=urlopen(url)import subprocessfrom PIL import Imagefrom PIL import ImageOpsdef cleanImage(imagePath): image=Image.open(imagePath) image.show() image=image.point(lambda x: 0 if x<50 else 255) borderImage=ImageOps.expand(image,border=20,fill=255) borderImage.save("1.gif")cleanImage(path)p=subprocess.Popen(["tesseract","1.gif","captcha"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)p.wait()f=open("captcha.txt","rb")b=f.read()s=b.decode()print(s.replace('\n','').replace(' ',''))

Python模拟登陆及验证码识别
Python模拟登陆及验证码识别

2、对校园网模拟登陆:这是模拟登陆,我把密码写成****涯箨唁峦**,不能泄露个人信息。采用 cookie打开链接,并post表单登陆。代码如下:Python 2.7import urllib2足毂忍珩import cookielibimport urllibimport reimport sysreload(sys)sys.setdefaultencoding("utf-8")checkCodenUrl="http://202.116.160.170/CheckCode.aspx"postUrl="http://202.116.160.170/default2.aspx"cookie=cookielib.CookieJar()handler=urllib2.HTTPCookieProcessor(cookie)opener=urllib2.build_opener(handler)username='201330160101'password='******'picture=opener.open(checkCodenUrl).read()local=open('C:\\Users\\jyjh\\Desktop\\1.jpg','wb')local.write(picture)local.close()checkCoden=raw_input('checkCoden:')postData = {'__VIEWSTATE': 'dDwyODE2NTM0OTg7Oz6XQwtkC4IPj2mY5bsI42qRkaJNzw==','txtUserName': username,'TextBox2': password,'txtSecretCode': checkCoden,'RadioButtonList1': '学生','Button1': '','lbLanguage': '','hidPdrs': '','hidsc': '',}headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Accept-Language': 'zh-CN,zh;q=0.8','Connection': 'keep-alive','Content-Type': 'application/x-www-form-urlencoded','User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36',}data = urllib.urlencode(postData)request = urllib2.Request(postUrl, data, headers)try: response=opener.open(request) result=response.read().decode('gb2312') print resultexcept urllib2.HTTPError as e: print e.code

Python模拟登陆及验证码识别
Python模拟登陆及验证码识别

3、post表单的数据来源于,使用抓包工具分析,分析表单传递的参数。

Python模拟登陆及验证码识别
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢