python图片验证码识别最新模块muggle_ocr的示例代码

 更新时间:2020年07月03日 08:48:28   作者:小小咸鱼YwY  
这篇文章主要介绍了python图片验证码识别最新模块muggle_ocr的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一.官方文档

https://pypi.org/project/muggle-ocr/

二模块安装

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

三.使用代码

# 导入包
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)

PS:下面看下 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实现模拟分割大文件及多线程处理的方法

    Python实现模拟分割大文件及多线程处理的方法

    这篇文章主要介绍了Python实现模拟分割大文件及多线程处理的方法,涉及Python文件读取、分割及多线程相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • python实现最小二乘法线性拟合

    python实现最小二乘法线性拟合

    这篇文章主要为大家详细介绍了python实现最小二乘法线性拟合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • 一文详解如何用GPU来运行Python代码

    一文详解如何用GPU来运行Python代码

    前几天捣鼓了一下Ubuntu,正是想用一下我旧电脑上的N卡,可以用GPU来跑代码,体验一下多核的快乐,感兴趣的小伙伴快跟随小编一起了解一下吧
    2023-02-02
  • Python使用Excel将数据写入多个sheet

    Python使用Excel将数据写入多个sheet

    这篇文章主要介绍了Python使用Excel将数据写入多个sheet,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • Python数据可视化处理库PyEcharts柱状图,饼图,线性图,词云图常用实例详解

    Python数据可视化处理库PyEcharts柱状图,饼图,线性图,词云图常用实例详解

    这篇文章主要介绍了Python数据可视化处理库PyEcharts柱状图、饼图、线性图常用实例详解,需要的朋友可以参考下
    2020-02-02
  • python中print格式化输出的问题

    python中print格式化输出的问题

    所谓格式化输出,就是创建一个可以嵌入变量内容的字符串。这篇文章主要介绍了python中print格式化输出,需要的朋友可以参考下
    2021-04-04
  • python矩阵基本运算的实现

    python矩阵基本运算的实现

    本文主要介绍了python 矩阵的基本运算,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • python 实现逻辑回归

    python 实现逻辑回归

    这篇文章主要介绍了python 实现逻辑回归的方法,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-12-12
  • 解决Pycharm出现的部分快捷键无效问题

    解决Pycharm出现的部分快捷键无效问题

    今天小编就为大家分享一篇解决Pycharm出现的部分快捷键无效问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • Python字符串的拆分与连接详解

    Python字符串的拆分与连接详解

    由于字符串数据几乎无处不在,因此掌握有关字符串的交易工具非常重要。幸运的是,Python 使字符串操作变得非常简单,尤其是与其他语言甚至旧版本的 Python 相比时。本文将为大家详细介绍Python中字符串的拆分与连接,需要的可以参考一下
    2021-12-12

最新评论