zoukankan      html  css  js  c++  java
  • Python3+pytesseract识别web验证码

    # -*- coding:utf-8 -*-
    #__author__ = "林深见鹿,海蓝见鲸"
    # date:2020-12-09
    # 识别图片验证码
    from selenium import webdriver
    import time
    from PIL import Image
    import os
    # import tesseract
    #解析图片验证码
    import pytesseract


    def Ver_code():
    driver = webdriver.Chrome(executable_path="C:Downloadchromedriver.exe")
    _url = "https://passport.51zhongzi.com/login"
    driver.get(_url)
    driver.maximize_window()
    driver.fullscreen_window()
    time.sleep(1)
    driver.find_element_by_name("az_username").send_keys(15000000000)
    driver.find_element_by_name("az_password").send_keys(111111)
    # 获取当前时间戳
    times = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
    print("获取当前时间戳:",times)
    # 获取当前工作目录
    path = "C:\Users\jiaow\Desktop\"
    img_path = path + times + ".png"
    print("验证码图片存放位置:",img_path)
    # 保存验证码图片
    driver.save_screenshot(img_path)
    # 获取图片的坐标元素
    code_element = driver.find_element_by_class_name("verifi-code")
    # 打印出 图片的坐标(.location方法)#(x:133,y:465)
    print("图片坐标:",code_element.location)
    left = code_element.location['x']
    top = code_element.location['y']
    right = code_element.size['width'] + left
    height = code_element.size['height'] + top
    # 打开保存的图片
    im = Image.open(img_path)
    # im = Image.convert()
    # 剪切图片尺寸
    img = im.crop((left, top, right, height))
    # 重新保存图片
    img.save(img_path)
    image = Image.open(img_path)
    image1 = Image.open("C:\Users\jiaow\Desktop\1.jpg") # 识别图片中的验证码并打印
    time.sleep(2)
    value = str(pytesseract.image_to_string(image))
    # value = str(pytesseract.image_to_string(image1))
    time.sleep(2)
    print("验证码:",value)
    if value is None:
    print("验证码为空")
    time.sleep(2)
    driver.find_element_by_name("az_code").send_keys(value)
    driver.find_element_by_class_name("login-btn").click()
    driver.quit()

    if __name__ == '__main__':
      Ver_code()
  • 相关阅读:
    redis 基本操作-python 使用redis-手机验证接口-发送短信接口
    登录-注册页面修订
    10-29 课堂笔记
    git 协作开发
    git 常规使用
    luffy-city 基础环境搭建(至轮播图前后台交互实现)-步骤目录
    偏移分页-游标(加密)分页-自定义过滤器-第三方过滤器插件(django-filter)
    drf 大总结
    739. Daily Temperatures
    617. Merge Two Binary Trees
  • 原文地址:https://www.cnblogs.com/jiaown123/p/14311552.html
Copyright © 2011-2022 走看看