识别简单验证码并进行提交

# -*-coding:utf-8 -*-
import re
import pytesseract
from PIL import Image
import requests
from contextlib import closing
import time
import datetime
import urllib2
import cookielib
import sys
reload(sys)
sys.setdefaultencoding('utf8')
#获取验证码的链接
#验证码的headers
headers={
'Host': 'www.mindai.com',
'Connection': 'keep-alive',
'Content-Length': '31',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Origin': 'https://www.mindai.com',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.2107.204 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': 'https://www.mindai.com/reg!regpage',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8'
}
#提交数据的headers
headers1={
'Host': 'www.mindai.com',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.2107.204 Safari/537.36',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8'
}
#验证码的cookies,保持cookies一致性
#code_url='https://www.mindai.com/rand'
#获取验证码
cookie=cookielib.CookieJar()
handler=urllib2.HTTPCookieProcessor(cookie)
opener=urllib2.build_opener(handler,urllib2.HTTPHandler)
urllib2.install_opener(opener)
class mingdai(object):
    def __init__(self):
        self.path='C:\\Users\\Administrator\\Desktop\\code\\'
        self.code_url='https://www.mindai.com/rand?d=0.9786535979000499'
    def getMessage(self,phone):
        session = requests.Session()
        response=session.get(self.code_url,headers=headers1,stream=True,verify=False)
        coo=response.cookies
        #print(response.cookies)
        #print(response)
        #print '测试成功'
        retry_time=0
        while 1:
            retry_time=retry_time+1
            #startTime = datetime.datetime.strptime(startTime, "%Y-%m-%d %H:%M:%S")
            with closing(session.get(self.code_url,headers=headers1,stream=True,verify=False)) as response1:
                with open(self.path+str(1)+'.jpg','wb') as fd:
                    for chunk in response1.iter_content(128):
                        fd.write(chunk)
            image=Image.open(self.path+str(1)+'.jpg')
            #pytesseract.image_to_string(image)
            print (pytesseract.image_to_string(image))
            string_code=pytesseract.image_to_string(image)
            url='https://www.mindai.com/reg!sendVerificationCode'
            postdat='phone='+str(phone)+'&valicode='+str(string_code)
            response1=session.post(url,headers=headers,data=postdat,verify=False)
            print(response1.text)
            data=response1.text
            if '该手机号已被注册' in data.encode('utf-8'):
                return  '该手机号已被注册'
            elif retry_time==5:
                return '验证码有误或者验证码为空'
f=mingdai()
data=f.getMessage(phone='15381971674')
print (data)

 

posted @ 2017-03-28 15:21  你若精彩,蝴蝶自来  阅读(559)  评论(0编辑  收藏  举报