码迷,mamicode.com
首页 > Windows程序 > 详细

百度api识别验证码登录

时间:2020-01-10 20:13:24      阅读:808      评论:0      收藏:0      [点我收藏+]

标签:图片   for   输入   ext   验证码识别   secret   import   try   code   

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()

百度api识别验证码登录

标签:图片   for   输入   ext   验证码识别   secret   import   try   code   

原文地址:https://www.cnblogs.com/Nayears/p/12177660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!