这篇教程python自动化调用百度api解决验证码写得很实用,希望能帮到您。 自动化测试验证码登陆的三种解决方式
1,找开发关闭验证码 2,找开发设置万能验证码 3,使用第三方接口识别验证–不能100%识别,比自己搭建的ocr识别的识别率高很多 具体讲的就是第三种-调用百度云识别验证码: from selenium import webdriverfrom PIL import Imageimport base64import requestsimport timedef baidu_api(Verification_code, AK, SK):#Verification_code验证码路径,AK,SK百度云的身份识别码 chrome.get_screenshot_as_file('reg.png') # 获取登陆页面的图片 code_img = chrome.find_element_by_xpath(Verification_code) # 找到验证码图片的位置 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'])) # 截取验证码图片 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')# 二进制方式打开图片文件 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])#数据类型的格式转换 dict_a = dict(dict_a)#转化为字典类型 dict_a = dict_a['words'] dict_a = "".join(dict_a.split()) # 使用一个空字符串合成列表内容生成新的字符串 dict_a = dict_a.lower()#把大写字母改为小写字母 return dict_a else: chrome.refresh()chrome = webdriver.Chrome()#浏览器实例化chrome.maximize_window()#最大化浏览器chrome.get('自己登陆的网址')test = baidu_api(Verification_code, AK, SK)#返回识别的验证码chrome = webdriver.Chrome()print(test)#验证码 百度云AK,SK的获取: 进入:百度云 点击立即使用——进行登陆——实名认证。

按着自己的需求选着,免费的基本上就够用了 
这就是自己的AK和SK 然后写入在代码里给AK和SK就行了 到此这篇关于python自动化调用百度api解决验证码的文章就介绍到这了,更多相关python调用百度api验证码内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net! 利用Python网络爬虫爬取各大音乐评论的代码 简述python四种分词工具,盘点哪个更好用? |