当前位置: 首页 > news >正文

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

文章目录

    • 摘要
    • 1、安装Tesseract-OCR
    • 2、在python中使用
      • 安装依赖
    • 3、本地图片识别
    • 4、结合playwright动态识别网站验证码

摘要

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

1、安装Tesseract-OCR

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

  • 下载地址:
    https://digi.bib.uni-mannheim.de/tesseract/
    选择最新的稳定版下载
  • 安装程序:下载后安装程序
  • 中文包下载:
    地址: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 filenameexcept 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 filenameexcept 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}")

相关文章:

  • 参数高效的提示调优实现了通用且校准的神经文本检索器
  • 网页自动化部署(webhook方法)
  • 【Go-补充】Sync包
  • STM32G4 电机外设篇(三) TIM1 发波 和 ADC COMP DAC级联
  • 【Go语言生态】
  • 结合源码分析Redis的内存回收和内存淘汰机制,LRU和LFU是如何进行计算的?
  • Spring Boot,注解,@ComponentScan
  • Mybtais框架各配置文件主要内容详解(一)
  • 【xmb】】内部文档148344599
  • Azure 机器学习初学者指南
  • PCIe—TS1/TS2 之Polling.Active(一)
  • MVVM、MVC的区别、什么是MVVM
  • VScode编译调试debug,gpu的cuda程序,Nsight
  • Amazon GameLift实战指南:低成本构建高并发全球游戏服务器架构
  • 【模拟电子电路-学习链接】
  • 杨校老师竞赛课之GESP一级C++[2024-12]真题及题解
  • latex 三线表-算法对比表
  • Leetcode 3566. Partition Array into Two Equal Product Subsets
  • STM32Cubemx-H7-17-麦克纳姆轮驱动
  • 设计模式——命令设计模式(行为型)
  • wordpress网站工具栏/推广普通话海报
  • 做的最少的网站/整站seo定制
  • 北京做网站设计招聘/网络营销有哪些特点
  • 查企业数据要去什么网站/长春网站建设策划方案
  • 凡科做的网站百度不到/软文代写文案
  • ps个人网站怎么做/seo包年优化平台