Python爬虫遇到验证码的几种处理方式,文章末尾有源码

开发 后端
本篇文章介绍了爬虫中验证码的处理方式, 并把这些功能封装起来,供我们使用, 涉及到百度AIP的调用方式, 以及一个最新的开源库muggle识别库的使用。

最近事情其实挺多了,打了一下蓝桥杯的比赛, 还在准备着一些证书的考试, 关于爬虫之类的博客都搁着了一段时间了, 关于我自己确实有点退步了, 实属不该, 其实我自己也是在想, 大三了,到底我是要去考研,还是依然像这样更新换代的学技术, 再或者, 继续钻爬虫这路子, 虽然我也不知道这路走的顺不顺, 自己也有点抓不住光明, 这段时间,大概花了一个多月的晚上吧, 终于把Django 的大致过了一次, 剩下的就是对着官方文档和一些实际项目操作了, 这些我也会打算开一个专栏,来专门记录一下我学习Django 的一些心酸道路, 学习依旧是这样, 你不学习,就会失去, 很是莫名其妙, 真的很奇怪, 某人的奖学金是靠关系的, 某项目的获奖者仅仅只是临时换了一个名字,。。。

不管这些了,无所谓的东西, 这边博客,将处理图片验证码的2个比较优秀的方式进行了一次封装, 分别是百度的aip 和 一个最近火起来的识别muggle-ocr

这里要主要提一下百度的aip,这里面的东西是真的多, 我还扩展了一个识别色情图片的函数, 有兴趣的可以玩一玩, 另外 学了爬虫之后, 这些图片真的是应接不暇, 网站也是多的数不胜数, 希望净网行动加把劲, 剩下的就不比比了, 看实际操作吧。

本篇文章介绍了爬虫中验证码的处理方式, 并把这些功能封装起来,供我们使用, 涉及到百度AIP的调用方式, 以及一个最新的开源库muggle识别库的使用。

目录:

  • 学会调用百度的aip接口:
  • 扩展百度的色情识别接口:
  • 学会muggle_ocr 识别接口:

封装源码:

学会调用百度的aip接口:

  • 1. 首先需要注册一个账号:
  • https://login.bce.baidu.com/

注册完成之后登入

2. 创建项目

在这些技术里面找到文字识别,然后点击创建一下项目

 

Python爬虫遇到验证码的几种处理方式,文章末尾有源码

创建完成之后:

 

Python爬虫遇到验证码的几种处理方式,文章末尾有源码

图片中 AppID , API key, Secret Key 这些待会是需要用的。

下一步可以查看官网文档,或者直接使用我写的代码

3. 安装一下依赖库 pip install baidu-aip

这只是一个接口, 需要前面的一些设置。

 

  1. def return_ocr_by_baidu(self, test_image): 
  2.         ""
  3.         ps: 先在__init__  函数中完成你自己的baidu_aip 的一些参数设置 
  4.  
  5.         这次测试使用 高精度版本测试 
  6.                     如果速度很慢 可以换回一般版本 
  7.                     self.client.basicGeneral(image, options) 
  8.                     相关参考网址: 
  9.                     https://cloud.baidu.com/doc/OCR/s/3k3h7yeqa 
  10.         :param test_image: 待测试的文件名称 
  11.         :return:  返回这个验证码的识别效果 如果错误  可以多次调用 
  12.         ""
  13.         image = self.return_image_content(test_image=self.return_path(test_image)) 
  14.  
  15.         # 调用通用文字识别(高精度版) 
  16.         # self.client.basicAccurate(image) 
  17.  
  18.         # 如果有可选参数 相关参数可以在上面的网址里面找到 
  19.         options = {} 
  20.         options["detect_direction"] = "true" 
  21.         options["probability"] = "true" 
  22.  
  23.         # 调用 
  24.         result = self.client.basicAccurate(image, options) 
  25.         result_s = result['words_result'][0]['words'
  26.         # 不打印关闭 
  27.         print(result_s) 
  28.         if result_s: 
  29.             return result_s.strip() 
  30.         else
  31.             raise Exception("The result is None , try it !"

扩展百度的色情识别接口:

我们写代码肯定是要找点乐子的, 不可能这么枯燥无味吧?

色情识别接口在 内容审核中, 找一下就可以了。

调用方式源码:

 

  1. # -*- coding :  utf-8 -*- 
  2. # @Time      :  2020/10/22  17:30 
  3. # @author    :  沙漏在下雨 
  4. # @Software  :  PyCharm 
  5. # @CSDN      :  https://me.csdn.net/qq_45906219 
  6.  
  7. from aip import AipContentCensor 
  8. from ocr import MyOrc 
  9.  
  10.  
  11. class Auditing(MyOrc): 
  12.     ""
  13.     这是一个调用百度内容审核的aip接口 
  14.     主要用来审核一些色情 反恐 恶心 之类的东西 
  15.     网址:  https://ai.baidu.com/ai-doc/ANTIPORN/tk3h6xgkn 
  16.     ""
  17.  
  18.     def __init__(self): 
  19.         # super().__init__() 
  20.         APP_ID = '填写你的ID' 
  21.         API_KEY = '填写你的KEY' 
  22.         SECRET_KEY = '填写你的SECRET_KEY' 
  23.  
  24.         self.client = AipContentCensor(APP_ID, API_KEY, SECRET_KEY) 
  25.  
  26.     def return_path(self, test_image): 
  27.         return super().return_path(test_image) 
  28.  
  29.     def return_image_content(self, test_image): 
  30.         return super().return_image_content(test_image) 
  31.  
  32.     def return_Content_by_baidu_of_image(self, test_image, mode=0): 
  33.         ""
  34.         继承ocr中的一些方法, 因为都是放一起的 少些一点代码 
  35.         内容审核: 关于图片中是否存在一些非法不良信息 
  36.         内容审核还可以实现文本审核 我觉得有点鸡肋  就没一起封装进去 
  37.         url: https://ai.baidu.com/ai-doc/ANTIPORN/Wk3h6xg56 
  38.         :param test_image: 待测试的图片 可以本地文件 也可以网址 
  39.         :param mode:  默认 = 0 表示 识别的本地文件   mode = 1 表示识别的图片网址连接 
  40.         :return: 返回识别结果 
  41.         ""
  42.         if mode == 0: 
  43.             filepath = self.return_image_content(self.return_path(test_image=test_image)) 
  44.         elif mode == 1: 
  45.             filepath = test_image 
  46.         else
  47.             raise Exception("The mode is 0 or 1 but your mode is ", mode) 
  48.         # 调用色情识别接口 
  49.         result = self.client.imageCensorUserDefined(filepath) 
  50.  
  51.         # """ 如果图片是url调用如下 """ 
  52.         # result = self.client.imageCensorUserDefined('http://www.example.com/image.jpg'
  53.         print(result) 
  54.         return result 
  55.  
  56.  
  57. a = Auditing() 
  58. a.return_Content_by_baidu_of_image("test_image/2.jpg", mode=0) 

学会muggle_ocr 识别接口:

这个包是最近火起来的, 使用起来很简单, 没多少其他函数

  • 安装 pip install muggle-ocr 这个下载有点慢 最好使用手机热点 目前镜像网站(清华/阿里) 还没有更新到这个包 因为这个包是最新的一个ocr模型 12
  • 调用接口

 

  1. def return_ocr_by_muggle(self, test_image, mode=1): 
  2.        ""
  3.            调用这个函数使用 muggle_ocr 来进行识别 
  4.            :param  test_image  待测试的文件名称 最好绝对路径 
  5.            :param  模型 mode = 0  即 ModelType.OCR 表示识别普通印刷文本 
  6.                  当 mode = 1 默认  即 ModelType.Captcha 表示识别4-6位简单英输验证码 
  7.  
  8.            官方网站: https://pypi.org/project/muggle-ocr/ 
  9.            :return: 返回这个验证码的识别结果 如果错误 可以多次调用 
  10.        ""
  11.        # 确定识别物品 
  12.        if mode == 1: 
  13.            sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha) 
  14.        elif mode == 0: 
  15.            sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.OCR) 
  16.        else
  17.            raise Exception("The mode is 0 or 1 , but your mode  == ", mode) 
  18.  
  19.        filepath = self.return_path(test_image=test_image) 
  20.  
  21.        with open(filepath, 'rb'as fr: 
  22.            captcha_bytes = fr.read() 
  23.            result = sdk.predict(image_bytes=captcha_bytes) 
  24.            # 不打印关闭 
  25.            print(result) 
  26.            return result.strip() 

封装源码:

 

  1. # -*- coding :  utf-8 -*- 
  2. # @Time      :  2020/10/22  14:12 
  3. # @author    :  沙漏在下雨 
  4. # @Software  :  PyCharm 
  5. # @CSDN      :  https://me.csdn.net/qq_45906219 
  6.  
  7. import muggle_ocr 
  8. import os 
  9. from aip import AipOcr 
  10.  
  11. ""
  12.     PS: 这个作用主要是作了一个封装 把2个常用的图片/验证码识别方式合在一起 怎么用 取决于自己 
  13.      
  14.     接口1: muggle_ocr  
  15.           pip install muggle-ocr 这个下载有点慢 最好使用手机热点 
  16.           目前镜像网站(清华/阿里)  还没有更新到这个包 因为这个包是最新的一个ocr模型 
  17.            
  18.     接口2: baidu-aip 
  19.           pip install baidu-aip 
  20.           这个知道的人应该很多很多, 但是我觉得还是muggle 这个新包猛的一比 
  21.           调用方式 可以参考官网文档: https://cloud.baidu.com/doc/OCR/index.html 
  22.           或者使用我如下的方式  都是ok的 
  23.     :param image_path  待识别的图片路径  如果目录很深 推荐使用绝对路径 
  24.      
  25. ""
  26.  
  27.  
  28. class MyOrc: 
  29.     def __init__(self): 
  30.         # 设置一些必要信息 使用自己百度aip的内容 
  31.         APP_ID = '你的ID' 
  32.         API_KEY = '你的KEY' 
  33.         SECRET_KEY = '你的SECRET_KEY' 
  34.  
  35.         self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY) 
  36.  
  37.     def return_path(self, test_image): 
  38.  
  39.         """:return abs image_path""" 
  40.         # 确定路径 
  41.         if os.path.isabs(test_image): 
  42.             filepath = test_image 
  43.         else
  44.             filepath = os.path.abspath(test_image) 
  45.         return filepath 
  46.  
  47.     def return_image_content(self, test_image): 
  48.         """:return the image content """ 
  49.         with open(test_image, 'rb'as fr: 
  50.             return fr.read() 
  51.  
  52.     def return_ocr_by_baidu(self, test_image): 
  53.         ""
  54.         ps: 先在__init__  函数中完成你自己的baidu_aip 的一些参数设置 
  55.  
  56.         这次测试使用 高精度版本测试 
  57.                     如果速度很慢 可以换回一般版本 
  58.                     self.client.basicGeneral(image, options) 
  59.                     相关参考网址: 
  60.                     https://cloud.baidu.com/doc/OCR/s/3k3h7yeqa 
  61.         :param test_image: 待测试的文件名称 
  62.         :return:  返回这个验证码的识别效果 如果错误  可以多次调用 
  63.         ""
  64.         image = self.return_image_content(test_image=self.return_path(test_image)) 
  65.  
  66.         # 调用通用文字识别(高精度版) 
  67.         # self.client.basicAccurate(image) 
  68.  
  69.         # 如果有可选参数 相关参数可以在上面的网址里面找到 
  70.         options = {} 
  71.         options["detect_direction"] = "true" 
  72.         options["probability"] = "true" 
  73.  
  74.         # 调用 
  75.         result = self.client.basicAccurate(image, options) 
  76.         result_s = result['words_result'][0]['words'
  77.         # 不打印关闭 
  78.         print(result_s) 
  79.         if result_s: 
  80.             return result_s.strip() 
  81.         else
  82.             raise Exception("The result is None , try it !"
  83.  
  84.     def return_ocr_by_muggle(self, test_image, mode=1): 
  85.         ""
  86.             调用这个函数使用 muggle_ocr 来进行识别 
  87.             :param  test_image  待测试的文件名称 最好绝对路径 
  88.             :param  模型 mode = 0  即 ModelType.OCR 表示识别普通印刷文本 
  89.                   当 mode = 1 默认  即 ModelType.Captcha 表示识别4-6位简单英输验证码 
  90.  
  91.             官方网站: https://pypi.org/project/muggle-ocr/ 
  92.             :return: 返回这个验证码的识别结果 如果错误 可以多次调用 
  93.         ""
  94.         # 确定识别物品 
  95.         if mode == 1: 
  96.             sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha) 
  97.         elif mode == 0: 
  98.             sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.OCR) 
  99.         else
  100.             raise Exception("The mode is 0 or 1 , but your mode  == ", mode) 
  101.  
  102.         filepath = self.return_path(test_image=test_image) 
  103.  
  104.         with open(filepath, 'rb'as fr: 
  105.             captcha_bytes = fr.read() 
  106.             result = sdk.predict(image_bytes=captcha_bytes) 
  107.             # 不打印关闭 
  108.             print(result) 
  109.             return result.strip() 
  110.  
  111.  
  112. # a = MyOrc() 
  113.  
  114. # a.return_ocr_by_baidu(test_image='test_image/digit_img_1.png'

 

责任编辑:未丽燕 来源: 今日头条
相关推荐

2021-08-02 12:29:15

Python爬虫网站

2021-06-10 18:24:59

反爬虫验证码爬虫

2020-04-26 14:40:19

戴尔

2013-06-19 10:19:59

2015-01-21 16:07:57

Android源码验证码倒计时

2021-01-19 10:29:34

短信验证码密码

2022-02-11 07:10:15

验证码

2020-11-16 07:28:53

验证码

2022-12-06 08:18:59

2011-11-02 16:46:41

2019-06-18 07:12:25

验证码漏洞加密

2011-11-02 12:43:33

2010-01-11 14:16:14

VB.NET生成验证码

2015-09-21 15:31:05

php实现验证码

2015-03-23 17:58:04

验证码倒计时并行

2017-12-21 07:38:19

2009-08-11 14:05:28

JSP验证码

2009-02-09 14:17:36

2024-01-29 08:32:10

Python验证码识别
点赞
收藏

51CTO技术栈公众号