• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    python3定位并识别图片验证码实现自动登录功能

    会用到的库的

    1、selenium的webdriver
    2、tesserocr或者pytesseract进行图像识别
    3、pillow的Image进行图片处理

    from selenium import webdriver
    import tesserocr
    from PIL import Image

    tesserocr的安装.

    获取验证码图片方法1:

    def get_code_image(file_name):
     driver.save_screenshot(file_name) # 截取整个屏幕并保存
     code_element = driver.find_element_by_class_name("verify_code_img___1Mei_") # 定位到验证码元素
     left = code_element.location['x'] # 定位到截图位置
     top = code_element.location['y']
     right = code_element.size['width'] + left
     bottom = code_element.size['height'] + top
     im = Image.open(file_name) # 从文件读取截图,截取验证码位置再次保存
     img = im.crop((left, top, right, bottom))
     img.save(file_name)
     return file_name

    获取验证码图片方法2:

    def get_code_image(file_name):
     code_element = driver.find_element_by_class_name("verify_code_img___1Mei_") # 定位到验证码元素 
     code_element.screenshot(file_name)

    注:此方法截图时屏幕会闪动,可能引发bug,如下图,目前没有解决

    处理验证码图片

    def deal_code_image(file_name):
     image = Image.open(file_name)
     # image.show() #查看处理前的图片
    	# 处理图片去除干扰
     # 将图片转化为灰度图像
     image = image.convert('L')
     
     threshold = 90 # 设置临界值,临界值可调试
     table = []
     for i in range(256):
      if i  threshold:
       table.append(0)
      else:
       table.append(1)
    
     image = image.point(table, '1')
     # image.show() #查看处理后的图片
     # 1:使用tesseract库识别图片中的验证码
     # res = tesserocr.image_to_text(image)
     # 2:使用pytesseract库识别图片中的验证码
     res = pytesseract.image_to_string(image)
    
     # print(res) #查看识别出来的文案
     res = res.replace(" ", "") #去除结果中的空格
     return res

    处理前的图片,有干扰,无法识别

    处理后的图片,基本可以识别

    识别结果不一定准确,如果验证码输入错误,可以点击换一张图片再次识别,多次尝试,本次不做说明

    到此这篇关于python3定位并识别图片验证码实现自动登录的文章就介绍到这了,更多相关python识别图片验证码实现自动登录内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • python爬取企查查企业信息之selenium自动模拟登录企查查
    • Appium+Python实现简单的自动化登录测试的实现
    • Python模拟键盘输入自动登录TGP
    • Python自动登录QQ的实现示例
    • Python 实现自动登录+点击+滑动验证功能
    • python自动化实现登录获取图片验证码功能
    • Python 自动登录淘宝并保存登录信息的方法
    • python实现网站用户名密码自动登录功能
    • python爬虫之利用selenium模块自动登录CSDN
    上一篇:python中numpy数组与list相互转换实例方法
    下一篇:Ubuntu20.04环境安装tensorflow2的方法步骤
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    python3定位并识别图片验证码实现自动登录功能 python3,定位,并,识别,图片,