Python解决同步验证码模拟登录问题
1、如何识别验证码前言:识别验证码借助第三方库很好识别 , 主要是获得验证码识别后又相当一次request请求 , 这就会导致与当前页面的验证码不同 , 即使成功识别也会因为验证码的不统一而登录不了
私信小编01即可获取大量Python学习资料
1.1模拟登录通过selenium自动化测试工具自动定位将用户名和密码输入后进入验证码的识别
文章插图
1.2验证码的获取(重点在于解决验证码同步避免两次请求)通过截屏将整页截图 , 再通过定位控件的位置以及crop函数截取想要的验证码区域
pic = driver.find_element_by_id('Image1')location = pic.locationsize = pic.size# 左上右下四元组坐标coordinates = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))driver.save_screenshot('test.png')i = Image.open('test.png')verfiy_pic = i.crop(coordinates)verfiy_pic.save('验证码.png')# 将同步的验证码保存到本地
1.3验证码的识别①通过OCR图片识别借助图鉴第三方打码平台识别(识别能力稍强 , 付费很便宜1块钱可以用几千次 , 也可能几百次反正很实惠 , 识别能力比②强)图鉴网站点击查看详情②(借助tesseract识别参考)(识别能力弱一点免费)接下来以第一种识别为例 , 账号自备
import jsonimport requestsimport base64from io import BytesIOfrom PIL import Imagefrom sys import version_infodef base64_api(uname, pwd, img):img = img.convert('RGB')buffered = BytesIO()img.save(buffered, format="JPEG")if version_info.major >= 3:b64 = str(base64.b64encode(buffered.getvalue()), encoding='utf-8')else:b64 = str(base64.b64encode(buffered.getvalue()))data = http://kandian.youth.cn/index/{"username": uname, "password": pwd, "image": b64}result = json.loads(requests.post("", json=data).text)if result['success']:return result["data"]["result"]else:return result["message"]return ""if __name__ == "__main__":img_path = "demo.jpg"img = Image.open(img_path)result = base64_api(uname='', pwd='', img=img)print(result)
2、整体代码不提供账户和密码等信息 , 代码提供完整的可根据需求自行变动
文章插图
文章插图
3、如何批量导入导出python库①导出:pip freeze > C:\Users\Administrator\Desktop\1.txt会在桌面生成一个txt文件里面有安装过的所有python第三方库
文章插图
【Python解决同步验证码模拟登录问题】②导入:pip install -r C:\Users\Administrator\Desktop\1.txt输入命令回车后文件中的所有库都会导入到site-package中(系统环境变量中)
推荐阅读
- 计算机专业大一下学期,该选择学习Java还是Python
- 大众展示EV公共充电新解决方案:移动充电机器人
- 与荷兰光刻机完成联机!国产芯片设备传来喜讯:技术问题已经解决
- 华为云知识计算解决方案获首批“知识图谱产品认证证书”
- 想自学Python来开发爬虫,需要按照哪几个阶段制定学习计划
- 未来想进入AI领域,该学习Python还是Java大数据开发
- 国产芯再传好消息,关键技术问题已经解决,与荷兰光刻机联机成功
- 2020百度地图生态大会:开放平台十周年 为行业送出多个解决方案“大礼包”
- 华为5G Core解决方案获2020通信产业金紫竹奖 优秀
- “机器人妻子”上市遭抢购,是在解决刚需,还是在挑战伦理?