2024-05-06 14:54:44 版本 : UIBOT利用python调用超级鹰识别验证码
作者: 周杨 于 2024年05月06日 发布在分类 / 计算机应用 / RPA 下,并于 2024年05月06日 编辑
 历史版本

备注 修改日期 修改人
格式调整 2024-05-06 15:16:35[当前版本] 周杨
格式调整 2024-05-06 15:16:03 周杨
格式调整 2024-05-06 15:14:54 周杨
格式调整 2024-05-06 15:02:27 周杨

UIBOT在识别登录验证码一般调用的MageAI接口,免费额度用完后目前无法续费(除非升级到企业用户),可以利用第三方平台进行识别,比如超级鹰(百度高精度OCR费用比较高,280元 1W次,12个月有效),以下就是利用官方python的demo代码(https://www.chaojiying.com/api-14.html)略加改造即可在RPA中调用,以下是官方的python代码,仅保留函数部分,比如保存为webtools.py:

1.改造代码

import requests  
from bs4 import BeautifulSoup
from hashlib import md5

'''
日期:2023-12
功能:验证网址URL是否能正常访问
更新:
    2024-01-11 增加超时参数,若10秒内没响应视为错误
# 示例用法 
urls = [  
    "https://www.google.com",  
    "https://www.example.com",  
    "https://www.notexistingwebsite.com"  
]  
  
for url in urls:  
    if check_website_access(url):  
        print(f"{url} 可以正常访问")  
    else:  
        print(f"{url} 无法正常访问")
'''
def check_website_access(url, timeout=10):  
    try: 
        headers = {
             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
        }
        response = requests.get(url,timeout=timeout,headers=headers,verify=False)  
        response.raise_for_status()  # 如果状态码不是200,将引发HTTPError异常  
        return "正常"  
    except requests.exceptions.RequestException as err:  
        return f"错误类型:{type(err).__name__}\n相关信息:{str(err)}"  
  


# get_main_doc_content(url,htmlLabel)获取网页指定标签<div class=htmlLabel></div>之间的源代码内容
def get_main_doc_content(url,htmlLabel):  
    # 发送HTTP请求获取网页内容  
    response = requests.get(url)  
      
    # 确保请求成功  
    if response.status_code != 200:  
        print(f"Failed to retrieve the webpage. Status code: {response.status_code}")  
        return None  
      
    # 解析网页内容  
    soup = BeautifulSoup(response.text, 'html.parser')  
      
    # 查找<div class="main__doc"></div>标签  
    main_doc_div = soup.find('div', class_= htmlLabel)  
      
    # 如果找到该标签,则返回其源代码;否则返回None  
    if main_doc_div:  
        return str(main_doc_div)  # 将BeautifulSoup对象转换为字符串  
    else:  
        return "没有找到class = " + htmlLabel 
    
#超级鹰验证码识别函数,使用法法如下:
'''
   chaojiying = Chaojiying_Client('超级鹰用户名', '超级鹰用户名的密码', '96001')	#用户中心>>软件ID 生成一个替换 96001
    im = open('a.jpg', 'rb').read()													#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    print(chaojiying.PostPic(im, 1902)['pic_id']	
'''
class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =  password.encode('utf8')
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.base_params = {
            'user': self.username,
            'pass2': self.password,
            'softid': self.soft_id,
        }
        self.headers = {
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        }

    def PostPic(self, im, codetype):
        """
        im: 图片字节
        codetype: 题目类型 参考 http://www.chaojiying.com/price.html
        """
        params = {
            'codetype': codetype,
        }
        params.update(self.base_params)
        files = {'userfile': ('ccc.jpg', im)}
        r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
        return r.json()

    def PostPic_base64(self, base64_str, codetype):
        """
        im: 图片字节
        codetype: 题目类型 参考 http://www.chaojiying.com/price.html
        """
        params = {
            'codetype': codetype,
            'file_base64':base64_str
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, headers=self.headers)
        return r.json()

    def ReportError(self, im_id):
        """
        im_id:报错题目的图片ID
        """
        params = {
            'id': im_id,
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
        return r.json()


然后在webtools.py文档尾部添加一个函数,get_verification_code,如下:


def get_verification_code(pic_path,code_type):
    chaojiying = ('超级鹰用户名', '超级鹰用户名的密码', '软件ID')	#用户中心>>软件ID 生成一个替换 96001
    im = open(pic_path, 'rb').read()													#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    return chaojiying.PostPic(im, code_type)['pic_str']
    # chaojiying.PostPic(im, code_type)实际返回值为一个json字符串:
    #{'err_no': 0, 'err_str': 'OK', 'pic_id': '1249811351827960001', 'pic_str': '3548', 'md5': 'c5de07ba71772ed32ec7fed559d2b8df'}  

   

将用户名、密码、软件ID填上并保存。

2.调用代码

(1)将webtools.py复制到RPA应用代码目录下的“extend\python”文件夹中(比如我的应用路径为:D:\UiBot\Projects\RPA\extend\python)

(2)建立“webtools.lib”文件夹,文件夹的名字为:py文件名+.lib

(3)将python调用的第三方库bs4文件夹复制到webtools.lib文件夹中

(4)在RPA流程块中调用

Import webTools
//用法:webTools.get_verification_code(pic_path,code_type),pic_path为图片路径,字符串类型,code_type为验证码类型,整数类型
//验证码类型详见:https://www.chaojiying.com/price.html

Dim pic_path

pic_path = 'D:\\UIBOT\\Projects\\RPA\\tmp\\verify.png' //界面操作→界面元素→元素截图中存放验证码图片的路径

sText = webTools.get_verification_code(pic_path,1004) //此处调用的“1~4位英文数字”类型


以上代码已在实际应用得到yan'zheng

历史版本-目录  [回到顶端]
    知识分享平台 -V 5.1.4 -大信谛威