robotframework环境搭建二十七:百度接口获取图片验证码

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/Allan_shore_ma/article/details/82770129

robotframework环境搭建二十七:百度接口获取图片验证码

获取图片验证码

思路

  1. 截图页面
  2. 地位验证码位置
  3. 保存验证码图片
  4. 调用百度接口获取验证码

关键字:GUI_Get_Verify_Code

在这里插入图片描述

百度接口

pip install baidu-aip
注册百度账号,百度云管理中心创建应用,生成AppID、App Key、Secret Key
参数:
APP_ID: 必须参数,固定为AppID;
API_KEY: 必须参数,应用的API Key;
SECRET_KEY: 必须参数,应用的Secret Key

pyhon 源代码

# coding=utf-8

# Author: Allan

import os
from PIL import Image
from aip import AipOcr


def get_image(path, filename, img, positions=None):
    #通过Image处理图像
    im = Image.open(os.path.join(path, filename))
    im = im.crop(positions)
    im.save(os.path.join(path, img))
    return 'PASS'


def GetCaptchaV(path, filename):
    #获取验证码
    APP_ID = 'xxx'
    API_KEY = 'yyy'
    SECRET_KEY = 'zzz'
 
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
 
    """ 读取图片 """
    def get_file_content(path, filename):
    	filename = os.path.join(path, filename)
        with open(filename, 'rb') as fp:
            return fp.read() 

    image = get_file_content(path, filename)
 
    """ 调用通用文字识别, 图片参数为本地图片 """
    ret = client.basicGeneral(image)
 
    code = ret["words_result"][0]["words"]
    print("Verify Code:",code)
    # 去掉空格
    if ' ' in code:
    	code = code.replace(' ', '')
    return code

猜你喜欢

转载自blog.csdn.net/Allan_shore_ma/article/details/82770129
今日推荐