python自動(dòng)化調(diào)用百度api解決驗(yàn)證碼
自動(dòng)化測試驗(yàn)證碼登陸的三種解決方式
1,找開發(fā)關(guān)閉驗(yàn)證碼2,找開發(fā)設(shè)置萬能驗(yàn)證碼3,使用第三方接口識(shí)別驗(yàn)證?不能100%識(shí)別,比自己搭建的ocr識(shí)別的識(shí)別率高很多
具體講的就是第三種-調(diào)用百度云識(shí)別驗(yàn)證碼:
from selenium import webdriverfrom PIL import Imageimport base64import requestsimport timedef baidu_api(Verification_code, AK, SK):#Verification_code驗(yàn)證碼路徑,AK,SK百度云的身份識(shí)別碼 chrome.get_screenshot_as_file(’reg.png’) # 獲取登陸頁面的圖片 code_img = chrome.find_element_by_xpath(Verification_code) # 找到驗(yàn)證碼圖片的位置 img = Image.open(’reg.png’)# 保存圖片 c_img = img.crop((code_img.location[’x’], code_img.location[’y’], code_img.location[’x’] + code_img.size[’width’], code_img.location[’y’] + code_img.size[’height’])) # 截取驗(yàn)證碼圖片 c_img.save(’reg_code.png’) host = ’https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&’ ’client_id=’+AK+’&’ ’client_secret=’+ SK response = requests.get(host) token = response.json()[’access_token’] request_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic' f = open(’reg_code.png’, ’rb’)# 二進(jìn)制方式打開圖片文件 img = base64.b64encode(f.read()) params = {'image': img} access_token = token request_url = request_url + '?access_token=' + access_token headers = {’content-type’: ’application/x-www-form-urlencoded’} response = requests.post(request_url, data=params, headers=headers) dict_a = response.json()[’words_result’] if response:dict_a = eval(str(dict_a)[1:-1])#數(shù)據(jù)類型的格式轉(zhuǎn)換dict_a = dict(dict_a)#轉(zhuǎn)化為字典類型dict_a = dict_a[’words’]dict_a = ''.join(dict_a.split()) # 使用一個(gè)空字符串合成列表內(nèi)容生成新的字符串dict_a = dict_a.lower()#把大寫字母改為小寫字母return dict_a else:chrome.refresh()chrome = webdriver.Chrome()#瀏覽器實(shí)例化chrome.maximize_window()#最大化瀏覽器chrome.get(’自己登陸的網(wǎng)址’)test = baidu_api(Verification_code, AK, SK)#返回識(shí)別的驗(yàn)證碼chrome = webdriver.Chrome()print(test)#驗(yàn)證碼
百度云AK,SK的獲取:進(jìn)入:百度云點(diǎn)擊立即使用——進(jìn)行登陸——實(shí)名認(rèn)證。
按著自己的需求選著,免費(fèi)的基本上就夠用了
這就是自己的AK和SK
然后寫入在代碼里給AK和SK就行了
到此這篇關(guān)于python自動(dòng)化調(diào)用百度api解決驗(yàn)證碼的文章就介紹到這了,更多相關(guān)python調(diào)用百度api驗(yàn)證碼內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. UDDI FAQs2. JSP之表單提交get和post的區(qū)別詳解及實(shí)例3. ASP基礎(chǔ)入門第三篇(ASP腳本基礎(chǔ))4. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)5. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲6. html清除浮動(dòng)的6種方法示例7. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法8. css進(jìn)階學(xué)習(xí) 選擇符9. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法10. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式
