Python解决同步验证码模拟登录问题

1、如何识别验证码前言:识别验证码借助第三方库很好识别 , 主要是获得验证码识别后又相当一次request请求 , 这就会导致与当前页面的验证码不同 , 即使成功识别也会因为验证码的不统一而登录不了
私信小编01即可获取大量Python学习资料
1.1模拟登录通过selenium自动化测试工具自动定位将用户名和密码输入后进入验证码的识别
Python解决同步验证码模拟登录问题文章插图
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、整体代码不提供账户和密码等信息 , 代码提供完整的可根据需求自行变动
Python解决同步验证码模拟登录问题文章插图
Python解决同步验证码模拟登录问题文章插图
3、如何批量导入导出python库①导出:pip freeze > C:\Users\Administrator\Desktop\1.txt会在桌面生成一个txt文件里面有安装过的所有python第三方库
Python解决同步验证码模拟登录问题文章插图
【Python解决同步验证码模拟登录问题】②导入:pip install -r C:\Users\Administrator\Desktop\1.txt输入命令回车后文件中的所有库都会导入到site-package中(系统环境变量中)


    推荐阅读