Python使用muggle_ocr模块识别图片验证码及Selenium自动化登录示例

拾光
拾光
发布时间:2026-10-14| 5人浏览过
原创

本文介绍Python中muggle_ocr模块的安装与使用方法,涵盖光学印刷文本与4-6位验证码两种识别模式。结合Selenium与PhantomJS实现网页自动登录,演示如何截取验证码图片并调用第三方接口或本地SDK进行识别,最终完成全自动登录流程。

muggle_ocr模块介绍与安装

一、官方文档

访问PyPI官方页面:https://pypi.org/project/muggle-ocr/

二、模块安装


pip install muggle-ocr
# 因模块过新,阿里/清华等第三方源可能尚未更新镜像,因此手动指定使用境外源,为了提高依赖的安装速度,可预先自行安装依赖:tensorflow/numpy/opencv-python/pillow/pyyaml

muggle_ocr基本使用示例

三、使用代码


# 导入包
import muggle_ocr

# 初始化;model_type 包含了 ModelType.OCR/ModelType.Captcha 两种
sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.OCR)
# ModelType.OCR 可识别光学印刷文本 这里个人觉得应该是官方文档写错了 官方文档是ModelType.Captcha 可识别光学印刷文本
with open(r"test1.png", "rb") as f:
 b = f.read()
text = sdk.predict(image_bytes=b)
print(text)

# ModelType.Captcha 可识别4-6位验证码
sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha)
with open(r"test1.png", "rb") as f:
 b = f.read()
text = sdk.predict(image_bytes=b)
print(text)

Selenium结合验证码识别实现自动登录

补充说明:以下示例展示如何使用Python实现全自动登录(真正的全自动,自动识别验证码)

你没看错,这就是全自动验证~~~

这是黑科技还是黑代码? 我觉得这取决于你的用途,对不对?反正我用来(* * * *)你懂得

好了,先说一下用到的东西

  • selenium(本意是用来全自动测试)
  • Phantomjs(一种没有界面的浏览器)
  • ** 验证码识别器(一块钱可用100次的这种)

关门放代码


from selenium import webdriver
from PIL import Image
if __name__ == '__main__':
 wbe = webdriver.PhantomJS()
 wbe.get("https://www.某个网站的登录页面.com/login/index.html")//你可以拿知乎,百度,等等测试
 element = wbe.find_element_by_xpath('//*[@id="entry_name"]/p[3]/img')//验证码所在的xpath路径
 left = element.location['x']
 top = element.location['y']
 right = element.location['x'] + element.size['width']
 bottom = element.location['y'] + element.size['height']
 im = Image.open(r'登录页.png')//全页面截屏
 im = im.crop((left, top, right, bottom))
 im.save('验证码.png')

#!/usr/bin/env python
# coding:utf-8
import requests
from hashlib import md5
class RClient(object):
 def __init__(self, username, password, soft_id, soft_key):
  self.username = username
  self.password = md5(password).hexdigest()
  self.soft_id = soft_id
  self.soft_key = soft_key
  self.base_params = {
   'username': self.username,
   'password': self.password,
   'softid': self.soft_id,
   'softkey': self.soft_key,
  }
  self.headers = {
   'Connection': 'Keep-Alive',
   'Expect': '100-continue',
   'User-Agent': 'ben',
  }
 def rk_create(self, im, im_type, timeout=60):
  """
  im: 图片字节
  im_type: 题目类型
  """
  params = {
   'typeid': im_type,
   'timeout': timeout,
  }
  params.update(self.base_params)
  files = {'image': ('a.png', im)}
  r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
  return r.json()
 def rk_report_error(self, im_id):
  """
  im_id:报错题目的ID
  """
  params = {
   'id': im_id,
  }
  params.update(self.base_params)
  r = requests.post('http://api.ruokuai.com/reporterror.json', data=params, headers=self.headers)
  return r.json()
def get_code():
 rc = RClient('用户名', '密码', '94522', '62c235939b7240879453f31603733fd6')//想拿下测试的留言我,教你拿到测试账号
 im = open('a.png', 'rb').read()
 print rc.rk_create(im, 3040)

完整代码


#!/usr/bin/env python
# coding:utf-8
from selenium import webdriver
from PIL import Image
import requests
from hashlib import md5
import time
class RClient(object):
 def __init__(self, username, password, soft_id, soft_key):
  self.username = username
  self.password = md5(password.encode("utf-8")).hexdigest()
  self.soft_id = soft_id
  self.soft_key = soft_key
  self.base_params = {
   'username': self.username,
   'password': self.password,
   'softid': self.soft_id,
   'softkey': self.soft_key,
  }
  self.headers = {
   'Connection': 'Keep-Alive',
   'Expect': '100-continue',
   'User-Agent': 'ben',
  }
 def rk_create(self, im, im_type, timeout=60):
  """
  im: 图片字节
  im_type: 题目类型
  """
  params = {
   'typeid': im_type,
   'timeout': timeout,
  }
  params.update(self.base_params)
  files = {'image': ('a.png', im)}
  r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
  return r.json()
 def rk_report_error(self, im_id):
  """
  im_id:报错题目的ID
  """
  params = {
   'id': im_id,
  }
  params.update(self.base_params)
  r = requests.post('http://api.ruokuai.com/reporterror.json', data=params, headers=self.headers)
  return r.json()
def get_code(im_file):
 rc = RClient('账号', '密码', '94522', '62c235939b7240879453f31603733fd6')
 im_source = open(im_file, "rb").read()
 print(rc.rk_create(im_source, 3040))
if __name__ == '__main__':
 wbe = webdriver.PhantomJS()
 wbe.get("https://www.dajiang365.com/login/index.html")
 time.sleep(2)
 wbe.save_screenshot("das.png")
 element = wbe.find_element_by_xpath('//*[@id="entry_name"]/p[3]/img')
 left = element.location['x']
 top = element.location['y']
 right = element.location['x'] + element.size['width']
 bottom = element.location['y'] + element.size['height']
 im = Image.open(r'das.png')
 im = im.crop((left, top, right, bottom))
 im.save('a.png')
 time.sleep(2)
 get_code("a.png")

总结

到此这篇关于python图片验证码识别最新模块muggle_ocr的示例代码的文章就介绍到这了,更多相关python 验证码识别模块muggle_ocr内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。