校验验证码 实现登录验证

Posted yuliangkaiyue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了校验验证码 实现登录验证相关的知识,希望对你有一定的参考价值。

验证码处理

方式

1.手动处理

2.云打码平台自动识别验证码

    实现流程:
    -1.对携带验证码的页面数据进行抓取
    -2.可以将验证码图片进行解析,验证码图片下载到本地
    -3.将验证码图片交给第三方进行识别,返回验证码图片上的值
        -云打码平台
        1.进行注册 注册普通用户和开发者用户
        2.登录开发者用户
            1.进行示例代码下载(开发文档》》调用示例及最新的DLL》》pythonhttp示例下载)
            2.创建一个软件(我的软件》》新的软件)
        3.使用示例代码中的源文件代码文件进行修改

代码实现

class YDMHttp:

    apiurl = http://api.yundama.com/api.php
    username = ‘‘
    password = ‘‘
    appid = ‘‘
    appkey = ‘‘

    def __init__(self, username, password, appid, appkey):
        self.username = username  
        self.password = password
        self.appid = str(appid)
        self.appkey = appkey

    def request(self, fields, files=[]):
        response = self.post_url(self.apiurl, fields, files)
        response = json.loads(response)
        return response
    
    def balance(self):
        data = {method: balance, username: self.username, password: self.password, appid: self.appid, appkey: self.appkey}
        response = self.request(data)
        if (response):
            if (response[ret] and response[ret] < 0):
                return response[ret]
            else:
                return response[balance]
        else:
            return -9001
    
    def login(self):
        data = {method: login, username: self.username, password: self.password, appid: self.appid, appkey: self.appkey}
        response = self.request(data)
        if (response):
            if (response[ret] and response[ret] < 0):
                return response[ret]
            else:
                return response[uid]
        else:
            return -9001

    def upload(self, filename, codetype, timeout):
        data = {method: upload, username: self.username, password: self.password, appid: self.appid, appkey: self.appkey, codetype: str(codetype), timeout: str(timeout)}
        file = {file: filename}
        response = self.request(data, file)
        if (response):
            if (response[ret] and response[ret] < 0):
                return response[ret]
            else:
                return response[cid]
        else:
            return -9001

    def result(self, cid):
        data = {method: result, username: self.username, password: self.password, appid: self.appid, appkey: self.appkey, cid: str(cid)}
        response = self.request(data)
        return response and response[text] or ‘‘

    def decode(self, filename, codetype, timeout):
        cid = self.upload(filename, codetype, timeout)
        if (cid > 0):
            for i in range(0, timeout):
                result = self.result(cid)
                if (result != ‘‘):
                    return cid, result
                else:
                    time.sleep(1)
            return -3003, ‘‘
        else:
            return cid, ‘‘

    def report(self, cid):
        data = {method: report, username: self.username, password: self.password, appid: self.appid, appkey: self.appkey, cid: str(cid), flag: 0}
        response = self.request(data)
        if (response):
            return response[ret]
        else:
            return -9001

    def post_url(self, url, fields, files=[]):
        for key in files:
            files[key] = open(files[key], rb);
        res = requests.post(url, files=files, data=fields)
        return res.text
# 调用打码平台,识别验证码
def getCode(codeImg):
    # 用户名
    username    = bobo328410948

    # 密码
    password    = bobo328410948                            

    # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
    appid       = 6003                                     

    # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
    appkey      = 1f4b564483ae5c907a1d34f8e2f2776c    

    # 图片文件
    filename    = codeImg   # 路径                       

    # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
    codetype    = 3000

    # 超时时间,秒
    timeout     = 20                                   

    # 检查
    if (username == username):
        print(请设置好相关参数再测试)
    else:
        # 初始化
        yundama = YDMHttp(username, password, appid, appkey)

        # 登陆云打码
        uid = yundama.login();
        print(uid: %s % uid)

        # 查询余额
        balance = yundama.balance();
        print(balance: %s % balance)

        # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
        cid, result = yundama.decode(filename, codetype, timeout);
        print(cid: %s, result: %s % (cid, result))
        
        return result
import requests
from lxml import etree
import json,time,re
#1.对携带验证码的页面数据进行抓取
url = https://accounts.douban.com/login?source=movie
# 自定义请求头信息
headers={
    User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

}
# 将代理IP封装到字典
proxies={
    http:59.44.43.198:80   # 更换代理IP可能会不出现验证码
}
page_text = requests.get(url=url,proxies=proxies).text
#2.可以将验证码图片进行解析,验证码图片下载到本地
tree = etree.HTML(page_text)
codeImg_url = tree.xpath(//*[@id="captcha_image"]/@src)[0]
#获取验证码图片对应的二进制数据
code_img = requests.get(url=codeImg_url,headers=headers).content
# 获取动态的captcha-id
captcha_id = re.findall(<img id="captcha_image".*?id=(.*?)&.*?>,page_text,re.S)[0]
with open(./code.png,wb)as fp:
    fp.write(code_img)
codeText = getCode(./code.png)
print(codeText)

# 进行登录操作
post = https://accounts.douban.com/login
data={
   source:movie,
    redir:https://movie.douban.com/,
    form_email:15027900535,
    form_password:[email protected],
    captcha-solution:codeText,
    captcha-id:captcha_id,
    login:登录
}

print(captcha_id)

login_text = requests.post(url=post,data=data,headers=headers,proxies=proxies).text
with open(./log.html,w,encoding=utf-8)as fp:
    fp.write(login_text)
print(ok)

 


以上是关于校验验证码 实现登录验证的主要内容,如果未能解决你的问题,请参考以下文章

vue element-ui 手机号校验 & 验证码校验 & 获取验证码倒数60秒无样式实现

SpringBoot+Vue项目中实现登录验证码校验

python登录加随机验证码校验程序(装饰器内置函数的理解)

javaweb中如何实时判定验证码的正误

Spring Security--短信验证码详解

登录添加验证码校验