zoukankan      html  css  js  c++  java
  • 识别简单验证码并进行提交

    # -*-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)
  • 相关阅读:
    多文件程序编译时头文件的使用方法
    360浏览器等被金山毒霸网强制霸占问题解决
    surface重装系统后,屏幕亮度不能调节,显示适配器出现黄色三角、windows hello不能正常使用
    Failed to execute "C:learnC程序练习1.exe": Error 0: 操作成功完成。 请按任意键继续. . .问题解决
    递归总结及斐波那契数列的实现
    在SpingBoot中使用Redis对接口进行限流
    在SpringBoot App中使用GoogleReCaptcha3过滤非法的请求
    在springboot中使用Guava基于令牌桶实现限流
    springboot jwt redis实现token刷新
    使用spring-validation和@RequestParam(required = false)字符串默认值的校验问题
  • 原文地址:https://www.cnblogs.com/caicaihong/p/6635021.html
Copyright © 2011-2022 走看看