当前位置: 代码网 > it编程>前端脚本>Python > Python验证码识别方式(使用pytesseract库)

Python验证码识别方式(使用pytesseract库)

2025年06月04日 Python 我要评论
python中使用pytesseract库进行ocr识别,需要安装tesseract-ocr,通过指定pytesseract.tesseract_cmd路径,可以将esseract-ocr集成到pyt

python中使用pytesseract库进行ocr识别,需要安装tesseract-ocr,通过指定pytesseract.tesseract_cmd路径,可以将esseract-ocr集成到pytho程序中,避免客户端电脑的依赖。

1、安装tesseract-ocr

tesseract是一个高度精确的开源ocr(光学字符识别)系统,广泛应用于文本识别项目中。

下载地址:

安装程序:下载后安装程序

中文包下载:

  • 地址:https://gitcode.com/open-source-toolkit/90e2f
  • 下载了最新版本的chi-sim.traineddata文件,复制到tesseract的tessdata目录下
  • 通常,路径类似于c:\program files\tesseract\tessdata(windows)
  • 或 /usr/share/tesseract-ocr/4.00/tessdata(linux)。

2、在python中使用

安装依赖

pip install pytesseract

3、本地图片识别

import pytesseract
from pil import image

# 获取文件的绝对路径
def get_abspath(filename):
    try:
        current_dir = os.getcwd()
        filename = os.path.normpath(os.path.join(current_dir, filename))
        # print(f"get_abspath文件路径:{filename}")
        return filename
    except exception as e:
        print(f"获取文件绝对路径时出现错误: {e}")
        return ""
        
# 手动指定路径(windows常见) tesseract 系统路径
driver_path = r"tesseract-ocr\\tesseract.exe"
pytesseract.pytesseract.tesseract_cmd = get_abspath(driver_path)

#使用示例
if __name__ == "__main__":
  # 1 识别本地图片 
  # 英文识别
  current_dir = os.getcwd()
    filename = os.path.normpath(os.path.join(current_dir, f"code.jpg"))
    file = image.open(filename)
    text = pytesseract.image_to_string(file, lang="eng")
    print(text)
  #中文识别,需要下载语言包
    filename = os.path.normpath(os.path.join(current_dir, f"sushi.png"))
    file = image.open(filename)
    text = pytesseract.image_to_string(file, lang='chi_sim') 
    print(f"识别结果:{text}")

识别结果示例:

4、结合playwright动态识别网站验证码

import os
import pytesseract
from pil import image
from playwright.sync_api import playwright
import tools.pwhander as pwhander
from pil import image

# 获取文件的绝对路径
def get_abspath(filename):
    try:
        current_dir = os.getcwd()
        filename = os.path.normpath(os.path.join(current_dir, filename))
        # print(f"get_abspath文件路径:{filename}")
        return filename
    except exception as e:
        print(f"获取文件绝对路径时出现错误: {e}")
        return ""
        
# 手动指定路径(windows常见) tesseract 系统路径
driver_path = r"tesseract-ocr\\tesseract.exe"
pytesseract.pytesseract.tesseract_cmd = get_abspath(driver_path)

# 验证码图片识别
def get_captcha(page: playwright, element_selector="img#captcha", file_name="code.jpg"):
    try:
        current_dir = os.getcwd()
        filename = os.path.normpath(os.path.join(current_dir, f"{file_name}"))
               
        # 通过class选择器获取img元素
        code_img = page.locator(element_selector)
        if not code_img:
            raise valueerror("验证码元素未找到!")

        # 刷新验证码
        # code_img.click()

        # 下载验证码图片
        code_img.screenshot(path=filename)

        file = image.open(filename)
        text = pytesseract.image_to_string(file, lang="eng")
        print("验证码识别结果:", text)
        return text.strip()
    except exception as e:
        print(f"获取验证码 失败:{str(e)}")
        return ""
#使用示例
if __name__ == "__main__":
  # 2 动态识别网站验证码
  with sync_playwright() as p:
   		browser = p.chromium.launch(headless=false, slow_mo=1000)
        context = browser.new_context()
        page = context.new_page()
        page.goto("测试网址")
	    # 验证码图片下载
	    imgtext = get_captcha(page, "img#jcaptcha")
	    print(f"验证码:{imgtest}")

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com