通过百度api和selenium实现验证码识别

import re
from aip import AipOcr
from selenium import webdriver
import time
from PIL import Image
from selenium.webdriver.common.by import By


def orc_test():
    driver = webdriver.Edge()
    driver.maximize_window()
    time.sleep(1)
    url = 'http://188.18.18.227:8091'
    driver.get(url)
    driver.save_screenshot('save_screenshot.png')

    code_ele = driver.find_element(By.ID, 's-canvas')

    right = code_ele.size['width'] + 1400  # 上面右边点的坐标
    down = code_ele.size['height'] + 380  # 下面右边点的坐标
    image = Image.open('save_screenshot.png')
    code_image = image.crop((1360, 380, right, down))
    code_image.save('code_image.png')

    APP_ID = ''
    API_KEY = ''
    SECRET_KEY = ''
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

    # 读取图片

    def get_file_content(file_path):
        with open(file_path, 'rb') as fp:
            return fp.read()

    image = get_file_content('code_image.png')

    # 调用通用文字识别(高精度版) """
    code_ocr_original_result = client.basicAccurate(image)
    code_ocr_final_result = ''
    for text in code_ocr_original_result['words_result']:
        # print(type(text))
        # print(text)
        for i in text.values():
            code_ocr_final_result = code_ocr_final_result + i
        # print(code_ocr_final_result)

    code_ocr_final_result_with_out = code_ocr_final_result.replace(' ', '')
    result = re.sub('[\W_]', '', code_ocr_final_result_with_out)
    return result


if __name__ == '__main__':
    print(orc_test())
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值