Python爬虫遇到验证码的几种处理方式,文章末尾有源码( 二 )
学会muggle_ocr 识别接口:这个包是最近火起来的 ,使用起来很简单 ,没多少其他函数
- 安装 pip install muggle-ocr 这个下载有点慢 最好使用手机热点 目前镜像网站(清华/阿里) 还没有更新到这个包 因为这个包是最新的一个ocr模型 12
- 调用接口
def return_ocr_by_muggle(self, test_image, mode=1):"""调用这个函数使用 muggle_ocr 来进行识别:paramtest_image待测试的文件名称 最好绝对路径:param模型 mode = 0即 ModelType.OCR 表示识别普通印刷文本当 mode = 1 默认即 ModelType.Captcha 表示识别4-6位简单英输验证码官方网站::return: 返回这个验证码的识别结果 如果错误 可以多次调用"""# 确定识别物品if mode == 1:sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha)elif mode == 0:sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.OCR)else:raise Exception("The mode is 0 or 1 , but your mode== ", mode)filepath = self.return_path(test_image=test_image)with open(filepath, 'rb') as fr:captcha_bytes = fr.read()result = sdk.predict(image_bytes=captcha_bytes)# 不打印关闭print(result)return result.strip()封装源码:# -*- coding :utf-8 -*-# @Time:2020/10/2214:12# @author:沙漏在下雨# @Software:PyCharm# @CSDN:import muggle_ocrimport osfrom aip import AipOcr"""PS: 这个作用主要是作了一个封装 把2个常用的图片/验证码识别方式合在一起 怎么用 取决于自己接口1: muggle_ocrpip install muggle-ocr 这个下载有点慢 最好使用手机热点目前镜像网站(清华/阿里)还没有更新到这个包 因为这个包是最新的一个ocr模型接口2: baidu-aippip install baidu-aip这个知道的人应该很多很多 ,但是我觉得还是muggle 这个新包猛的一比调用方式 可以参考官网文档:或者使用我如下的方式都是ok的:param image_path待识别的图片路径如果目录很深 推荐使用绝对路径"""class MyOrc:def __init__(self):# 设置一些必要信息 使用自己百度aip的内容APP_ID = '你的ID'API_KEY = '你的KEY'SECRET_KEY = '你的SECRET_KEY'self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY)def return_path(self, test_image):""":return abs image_path"""# 确定路径if os.path.isabs(test_image):filepath = test_imageelse:filepath = os.path.abspath(test_image)return filepathdef return_image_content(self, test_image):""":return the image content """with open(test_image, 'rb') as fr:return fr.read()def return_ocr_by_baidu(self, test_image):"""ps: 先在__init__函数中完成你自己的baidu_aip 的一些参数设置这次测试使用 高精度版本测试如果速度很慢 可以换回一般版本self.client.basicGeneral(image, options)相关参考网址::param test_image: 待测试的文件名称:return:返回这个验证码的识别效果 如果错误可以多次调用"""image = self.return_image_content(test_image=self.return_path(test_image))# 调用通用文字识别(高精度版)# self.client.basicAccurate(image)# 如果有可选参数 相关参数可以在上面的网址里面找到options = {}options["detect_direction"] = "true"options["probability"] = "true"# 调用result = self.client.basicAccurate(image, options)result_s = result['words_result'][0]['words']# 不打印关闭print(result_s)if result_s:return result_s.strip()else:raise Exception("The result is None , try it !")def return_ocr_by_muggle(self, test_image, mode=1):"""调用这个函数使用 muggle_ocr 来进行识别:paramtest_image待测试的文件名称 最好绝对路径:param模型 mode = 0即 ModelType.OCR 表示识别普通印刷文本当 mode = 1 默认即 ModelType.Captcha 表示识别4-6位简单英输验证码官方网站::return: 返回这个验证码的识别结果 如果错误 可以多次调用"""# 确定识别物品if mode == 1:sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha)elif mode == 0:sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.OCR)else:raise Exception("The mode is 0 or 1 , but your mode== ", mode)filepath = self.return_path(test_image=test_image)with open(filepath, 'rb') as fr:captcha_bytes = fr.read()result = sdk.predict(image_bytes=captcha_bytes)# 不打印关闭print(result)return result.strip()# a = MyOrc()# a.return_ocr_by_baidu(test_image='test_image/digit_img_1.png')
- 告诉|阿里大佬告诉你如何一分钟利用Python在家告别会员看电影
- Python源码阅读-基础1
- Python调用时使用*和**
- 如何基于Python实现自动化控制鼠标和键盘操作
- 解决多版本的python冲突问题
- 学习python第二弹
- Python中文速查表-Pandas 基础
- 零基础小白Python入门必看:通俗易懂,搞定深浅拷贝
- Python 使用摄像头监测心率!这么强吗?
- 十分钟教会你使用Python操作excel,内附步骤和代码
