selenium--自动化识别图片验证码并输入

首先注册百度智能云账号(这里我用的是百度智能云):

selenium--自动化识别图片验证码并输入_第1张图片

 

 1.要在这里面保存好API Key 和 Secret Key

 2.然后进入查看文档

selenium--自动化识别图片验证码并输入_第2张图片

 

在pycharm中:

import requests
import redis
import base64

# 封装百度类
class Baidu:
    def __init__(self):

        # apikey 写入自己先前保存好的
        self.apikey = ""
        # api secretkey
        self.apisecret = ""

        self.redis = redis.Redis(password="123",decode_responses=True)


    # 文字图片识别
    def cor(self, filename="./test.png"):

        # 定义请求地址
        request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
        # 读取图片
        f = open(filename, 'rb')
        # base64编码
        img = base64.b64encode(f.read())
        # 定义请求参数
        params = {"image": img}

        access_token = self.redis.get("baidutoken")

        request_url = request_url + "?access_token=" + access_token
        headers = {'content-type': 'application/x-www-form-urlencoded'}
        # 发起请求
        response = requests.post(request_url, data=params, headers=headers)
        if response:
            print(response.json())
            # 获取识别的结果
            num = ""
            for x in response.json()["words_result"]:
                num += x["words"]

            return num

    # 获取token
    def get_token(self):
        host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s' \
               '&client_secret=%s' % (self.apikey,self.apisecret)
        response = requests.get(host)
        if response:
            print(response.json())
            # 存储token
            try:
                self.redis.set("baidutoken",response.json()["access_token"])
            except Exception as e:
                print("请求报错,无法获取token")



if __name__ == '__main__':
    baidu = Baidu()
    print(baidu.cor())

导入自动化文件:

from baiduapi import Baidu

在写自动化脚本时调用封装的百度类:

注意:自动化识别文字可能会出现识别不出来的可能,要解决这个Bug。

代码分享:

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from time import sleep

from baiduapi import Baidu
from selenium.webdriver.edge.options import Options
# 设置配置
myoptions = Options()
myoptions.add_experimental_option("detach",True)

# 自动化注册

class AutoReg:

    # 初始化方法
    def __init__(self):
        self.url = "http://localhost:8080/reg"
        self.email = "123"
        self.password = "123"

        # 初始化浏览器实例
        self.browser = webdriver.Edge()
        # 打开网页
        self.browser.get("http://localhost:8080/reg")

    # 注册动作
    def first(self):
        # 延迟
        time.sleep(2)
        # 填写表单
        self.browser.find_elements(By.TAG_NAME, "input")[0].send_keys("123")
        self.browser.find_elements(By.TAG_NAME, "input")[1].send_keys("123")
        # 生成验证码
        self.imgcode()
        # 识别
        num = self.cor()

        # 写入验证码

        if num:
            self.send(num)

    # 写入验证码
    def send(self,num):
        self.browser.find_elements(By.TAG_NAME, "input")[2].send_keys(num)
        self.browser.find_elements(By.CLASS_NAME,"van-button")[1].click()

    # 重新生成图片验证码
    def imgcode(self):
        # 生成验证码
        self.browser.find_elements(By.CLASS_NAME, "van-button")[0].click()
        time.sleep(2)

        # 选择随机图片,截图
        img = self.browser.find_elements(By.TAG_NAME, "img")[0]

        # 截图该元素图片
        img.screenshot("test.png")

    # 识别方法
    def cor(self):

        # 实例化对象
        baidu = Baidu()
        num = baidu.cor()

        times = 0
        success = False

        while times<3 and not success:

            print("第%s次" % (times+1))

            num = baidu.cor()
            if len(num) == 4:
                success = True
            else:
                self.imgcode()
            times += 1
        if success:
            return num
        else:
            return None



    # 析构方法
    def __del__(self):

        time.sleep(3)
        self.browser.quit()






if __name__ == '__main__':
    autoreg = AutoReg()
    autoreg.first()
    exit(-1)

selenium--自动化识别图片验证码并输入_第3张图片

 

其中涉及到页面截屏功能,根据当前页面来识别图片验证码,进行输入,要考虑好逻辑,避免出现

识别不出来的Bug!!!

你可能感兴趣的:(python-tornado,selenium,自动化,大数据,python,tornado)