• Python之验证码识别功能


    Python之pytesseract 识别验证码

    1、验证码来一个

     2、适合什么样的验证码呢?

    只能识别简单、静态、无重叠、只有数字字母的验证码

    3、实际应用:模拟人工登录、页面内容识别、爬虫抓取信息

    步骤一:

    下载工具Tesseract-OCR,下载地址https://digi.bib.uni-mannheim.de/tesseract/,下载成功后,傻瓜式安装在英文路径下

    安装后或出现一个目录:D:syspath esseractTesseract-OCR,将安装路径配置环境变量

    步骤二:

    添加tessdata系统变量,要注意是系统变量:名称TESSDATA_PREFIX;路径D:syspath esseractTesseract-OCR essdata

    dos命令窗口输入:tesseract --version   展示版本信息后说明OK

    验证图片识别:

    同样dos命令窗口:tesseract C:a.png D:1.txt

    a.png是图片,1.txt是识别后数据存放文件

    如何识别页面上的信息并抓取信息

    
    
    安装pytesserac模块:pip install pytesserac
    修改pytesseract.py文件中tesseract_cmd字段信息
    例如:将tesseract_cmd = 'tesseract'修改为:tesseract_cmd = 'D:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
    这样就安装好pytesserac模块了
    使用:
    一个获取登陆页面验证码的脚本#-*- coding = utf - 8 -*-

    import time , sys
    import pytesseract
    from PIL import Image,ImageEnhance
    from selenium import webdriver
    screenImg = "D:/checkcode.png"
    driver = webdriver.Chrome
    driver.get(https://192.168.1.1:8080/login)
    driver.maximize_window()
    driver.save_screenshot(screenImg) # 截取当前网页,该网页有我们需要的验证码
    location = self.param1.find_element_by_xpath("//img[@class='check-code']").location
    size = self.param1.find_element_by_xpath("//img[@class='check-code']").size
    left = location['x']
    top = location['y']
    right = location['x'] + size['width']
    bottom = location['y'] + size['height']
    img = Image.open(screenImg).crop((left, top, right, bottom))
    img = img.convert('L') # 转换模式:L | RGB
    img = ImageEnhance.Contrast(img) # 增强对比度
    img = img.enhance(2.0) # 增加饱和度
    img.save(screenImg)
    # 读取截图
    img = Image.open(screenImg)
    # 获取验证码
    code = pytesseract.image_to_string(img)
    # 打印验证码
    print(list(code))


    以上是抓取一个页面的验证码,那么同样可以使用在爬虫上面去
    楼主萌新,不喜勿喷 哈哈哈
  • 相关阅读:
    Pycharm
    Python
    navicat连接MySQL8.0出现2059错误
    MySQL Community Server 8.0.11下载与安装配置
    pip升级以及导入模块
    pycharm安装
    python环境安装
    js 超级玛丽(未完成)
    js 点名
    js 获取鼠标位置坐标
  • 原文地址:https://www.cnblogs.com/zhangqinANDwangjiasen/p/12068598.html
Copyright © 2020-2023  润新知