Selenium之自动登录(识别验证码版)


备注:

  • 测试不一定成功(图像识别技术...),多次尝试
  • 驱动:Chrome驱动
  • 图片识别:百度AI提供(APP_ID、API_KEY、SECRET_KEY均由百度AI产生)
  • 图像识别过程会在本地保存验证码图片
  • 测试网站:https://pythonav.com/login/
```python 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 selnium_msg():
driver = webdriver.Chrome()
try:
driver.get('https://pythonav.com/login/')
time.sleep(1)

    # 输入用户名/密码
    driver.find_element_by_id('id_username').send_keys('用户名')
    time.sleep(2)
    driver.find_element_by_id('id_password').send_keys('密码')
    time.sleep(2)

    # 验证码识别
    image = driver.find_element_by_id('image_code')
    file_path = 'a.png'
    image.screenshot(file_path)

    # 百度ai相关
    client = initial()
    image = get_file_content(file_path)
    res = client.basicGeneral(image)  # 调用通用文字识别, 图片参数为本地图片
    # res2 = client.basicAccurate(image)  # 调用通用文字识别(高精度版)

    driver.find_element_by_id('id_code').send_keys(res['words_result'][0]['words'])
    driver.find_element_by_class_name('btn-primary').click()
    time.sleep(3)
except Exception as e:
    print(e)
finally:
    driver.quit()

if name == 'main':
selnium_msg()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM