先安装对应模块:
#pip install baidu-aip 百度AI
#pip install pillow 图片处理
#coding=utf-8 #pip install baidu-aip 百度AI #pip install pillow 图片处理 from selenium import webdriver import time from aip import AipOcr from PIL import Image,ImageEnhance,ImageDraw import json,re #通过百度AI识别图片验证码 """ 读取图片 """ def get_file_content(filePath): with open(filePath, ‘rb‘) as fp: return fp.read() """ 读取图片 """ def GetIdentifying_code():#识别验证码 APP_ID = "16630245" API_KEY = "0uyemPLBBweQnmYYn8YSfpM8" SECRET_KEY = "NXyk8t93E0Af7qMOI2tRQxYqkKspGK7L" client = AipOcr(APP_ID, API_KEY, SECRET_KEY) """ 读取图片 """ image = get_file_content(‘image_code.png‘) """ 调用通用文字识别, 图片参数为本地图片 """ options = {} options["language_type"] = "CHN_ENG" options["detect_direction"] = "true" options["detect_language"] = "true" options["probability"] = "true" """ 带参数调用通用文字识别, 图片参数为本地图片 """ result=client.basicGeneral(image, options) #print(result) pattern6="‘words‘: (.*?),"#正则匹配过滤数据 result6=re.compile(pattern6).findall(str(result)) try: Coderesult=result6[0].replace("‘","") Coderesult=Coderesult.replace("}","") Coderesult=Coderesult.replace("]","") except: Coderesult=‘不识别‘ print(Coderesult) return Coderesult def GetIdentifying_codePIC(): im =Image.open(‘image1.jpg‘) # box = (1000,420,1250,490) #设置要裁剪的区域 box = (800,420,910,470) #设置要裁剪的区域 region = im.crop(box) #此时,region是一个新的图像对象。 #图片处理 region.save("image_code.png") driver = webdriver.Chrome() driver.get(‘http://192.168.7.2:18080/jportal/index.html#/login‘) time.sleep(2) driver.find_element_by_xpath(‘//*[@id="wrapLogin"]/div[3]/section[2]/form/div[1]/div/div/input‘).send_keys("000002") driver.find_element_by_xpath(‘//*[@id="wrapLogin"]/div[3]/section[2]/form/div[2]/div/div/input‘).send_keys("888888") driver.find_element_by_xpath(‘//*[@id="wrapLogin"]/div[3]/section[2]/form/div[3]/div/div/div[2]/img‘).click()#切换验证码 driver.get_screenshot_as_file(‘image1.jpg‘) GetIdentifying_codePIC() time.sleep(1) code=GetIdentifying_code() driver.find_element_by_xpath(‘//*[@id="wrapLogin"]/div[3]/section[2]/form/div[3]/div/div/div[1]/div/input‘).clear() driver.find_element_by_xpath(‘//*[@id="wrapLogin"]/div[3]/section[2]/form/div[3]/div/div/div[1]/div/input‘).send_keys(code) time.sleep(1) driver.find_element_by_xpath(‘//*[@id="wrapLogin"]/div[3]/section[2]/form/div[4]/div/button‘).click()#登录 time.sleep(1) driver.quit()
原文:https://www.cnblogs.com/biqiuqiu/p/11081966.html