robotframework环境搭建二十八:百度接口获取图片验证码【2】
robotframework环境搭建二十八:百度接口获取图片验证码【2】
问题:百度接口 baidu-aip 识别图片效果不佳
百度提供其他高精度识别图片接口 https://cloud.baidu.com/doc/OCR/OCR-API.html#.E8.AF.B7.E6.B1.82.E6.A0.BC.E5.BC.8F
通过 python 的 requests 库实现获取验证码
获取图片验证码
思路
- 截图页面
- 地位验证码位置
- 保存验证码图片
- 调用百度接口获取验证码
关键字:GUI_Get_Verify_Code
百度接口
手写文字识别:https://cloud.baidu.com/doc/OCR/OCR-API.html#.E6.89.8B.E5.86.99.E6.96.87.E5.AD.97.E8.AF.86.E5.88.AB
注册百度账号,百度云管理中心创建应用,生成AppID、App Key、Secret Key
参数:
API_KEY: 必须参数,应用的API Key;
SECRET_KEY: 必须参数,应用的Secret Key
image:图像的 base64 编码
language_type:语言类型
pyhon 源代码
# coding=utf-8
# Author: Allan
# Version: 2.0
import os
from PIL import Image
import requests
import base64
import json
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 get_file_content(path, filename):
""" 读取图片 """
filename = os.path.join(path, filename)
with open(filename, 'rb') as fp:
return fp.read()
def GetCaptchaV(path, filename):
"""
获取验证码
Arguments:
| path | 路径 |
Arguments:
| filename | 文件名 |
Examples:
| GetCaptchaV | path | verify.png |
"""
url_post = 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting'
image = get_file_content(path, filename)
image = base64.b64encode(image)
access_token = get_access_token()
data = {'access_token': access_token, 'image': image, 'language_type': 'CHN_ENG'}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
post_req = requests.post(url_post, headers=headers, data=data)
res = post_req.text
res = json.loads(res, encoding='utf-8')
if res['words_result']:
# 防止为空,报异常
code = res['words_result'][0]['words']
if ' ' in code:
# 去掉空格
code = code.replace(' ', '')
return code
return '0'
def get_access_token():
""" 获取百度访问token """
API_KEY = 'xxx'
SECRET_KEY = 'yyy'
url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(API_KEY, SECRET_KEY)
res = requests.post(url)
res = json.loads(res.text, encoding='utf-8')
return res['access_token']