您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:python自动化调用百度api解决验证码

51自学网 2021-10-30 22:45:55
  python
这篇教程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四种分词工具,盘点哪个更好用?
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。