Particles

百度api识别验证码登录

import time
from selenium import webdriver
from aip import AipOcr

def initial():
    """ 初始化连接 """
    APP_ID = '16611607'
    API_KEY = 'wAIXfXOUS8ztLa4FrK3rZex1'
    SECRET_KEY = '3b8nvjSGUZq0LPC18VVAizKYRBbny6Mq'
    return AipOcr(APP_ID, API_KEY, SECRET_KEY)
    
def get_file_content(filePath):
    """ 读取图片 """
    with open(filePath, 'rb') as f:
        return f.read()
        
def selenium_msg():
    driver = webdriver.Chrome()
    try:
        driver.get('https://pythonav.com/login/')
        time.sleep(1)

        # 输入用户密码
        driver.find_element_by_id('id_username').send_keys('python')
        time.sleep(1)
        driver.find_element_by_id('id_password').send_keys('dsb123')
        time.sleep(2)

        # 验证码识别
        file_path = 'b.png' 
        image = driver.find_element_by_id('image_code')  #找到验证码 
        image.screenshot(file_path)   #保存验证码图片

        # 百度ai相关
        client = initial()
        image = get_file_content(file_path)   #读取图片文件
        res1 = client.basicGeneral(image)  # 调用通用文字识别, 图片参数为本地图片
        # res2 = client.basicAccurate(image)  # 调用通用文字识别(高精度版)
        a = ''
        for text in res1['words_result']:
            a += text['words']
        time.sleep(2)
        driver.find_element_by_id('id_code').send_keys(a)
        time.sleep(2)
        driver.find_element_by_class_name('btn-primary').click()  #登录按钮进行点击
    finally:
        time.sleep(5)
        driver.quit()
if __name__ == '__main__':
    selenium_msg()
posted @ 2020-01-10 18:49  Na_years  阅读(1227)  评论(0编辑  收藏  举报