Python crawler encounters several ways to deal with the verification code, the source code is at the end of the article

Recently, things have been quite a lot. I played a game in the Blue Bridge Cup, and I am still preparing for some certificate exams. Blogs about reptiles have been on hold for a while. I have really regressed a little bit about myself, but it’s not true. Oh, in fact, I’m also thinking, in my junior year, whether I’m going to take the postgraduate entrance examination, or will I still learn the new technology like this, or, continue the crawler road, although I don’t know whether this road goes smoothly or not. By the way, I can’t catch the light. During this period of time, it took me more than a month. I finally passed Django's roughly once. The rest is to operate on the official documents and some actual projects. I also plan to open a column to specifically record some of my sad paths in learning Django. Learning is still the same. If you don’t study, you will lose it. It’s very inexplicable and really strange. Someone’s scholarship depends on the relationship. The winner of a certain project just changed a name temporarily. . .

Many people learn python and don't know where to start.

Many people learn python and after mastering the basic grammar, they don't know where to find cases to get started.

Many people who have done case studies do not know how to learn more advanced knowledge.

So for these three types of people, I will provide you with a good learning platform, free to receive video tutorials, e-books, and the source code of the course! ??¤

QQ group: 701698587

 


Regardless of this, it doesn’t matter. This blog encapsulates two more excellent ways of processing image verification codes, namely Baidu’s aip and a recently popular recognition muggle-ocr.
Here I will mainly mention Baidu. There are so many things in it. I also expanded a function to identify pornographic pictures. Those who are interested can have fun. After learning crawlers, these pictures are really overwhelming and there are countless websites. I hope that the net-net operation will be more vigorous, and the rest will not be compared. Let's see the actual operation.


This article introduces the processing method of the verification code in the crawler, and encapsulates these functions for us to use, involving the call method of Baidu AIP, and the use of the latest open source library muggle recognition library. Welcome to read and like it. Collection!

Other articles of the blogger, welcome to read!


table of Contents:

  • Learn to call Baidu's aip interface:

  • Expansion of Baidu's porn recognition interface:

  • Learn muggle_ocr to identify the interface:

  • Package source code:


Learn to call Baidu's aip interface:

1. First, you need to register an account:

https://login.bce.baidu.com/

注册完成之后登入

2. Create the project

Find text recognition in these technologies, and click to create a project

image

 

After creation:

image

 

The AppID, API key, and Secret Key in the picture will be needed later.

Next, you can check the official website document, or use the code I wrote directly

3. Install the dependency library pip install baidu-aip

This is just an interface and requires some previous settings.

 def return_ocr_by_baidu(self, test_image):
        """
        ps: 先在__init__  函数中完成你自己的baidu_aip 的一些参数设置

        这次测试使用 高精度版本测试
                    如果速度很慢 可以换回一般版本
                    self.client.basicGeneral(image, options)
                    相关参考网址:
                    https://cloud.baidu.com/doc/OCR/s/3k3h7yeqa
        :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 !")

Expansion of Baidu's porn recognition interface:

We must have some fun writing code, it can't be so boring, right?

The pornographic identification interface is in the content review process, just look for it.

Source code of calling method:

# -*- coding :  utf-8 -*-
# @Time      :  2020/10/22  17:30
# @author    :  沙漏在下雨
# @Software  :  PyCharm
# @CSDN      :  https://me.csdn.net/qq_45906219

from aip import AipContentCensor
from ocr import MyOrc


class Auditing(MyOrc):
    """
    这是一个调用百度内容审核的aip接口
    主要用来审核一些色情 反恐 恶心 之类的东西
    网址:  https://ai.baidu.com/ai-doc/ANTIPORN/tk3h6xgkn
    """

    def __init__(self):
        # super().__init__()
        APP_ID = '填写你的ID'
        API_KEY = '填写你的KEY'
        SECRET_KEY = '填写你的SECRET_KEY'

        self.client = AipContentCensor(APP_ID, API_KEY, SECRET_KEY)

    def return_path(self, test_image):
        return super().return_path(test_image)

    def return_image_content(self, test_image):
        return super().return_image_content(test_image)

    def return_Content_by_baidu_of_image(self, test_image, mode=0):
        """
        继承ocr中的一些方法, 因为都是放一起的 少些一点代码
        内容审核: 关于图片中是否存在一些非法不良信息
        内容审核还可以实现文本审核 我觉得有点鸡肋  就没一起封装进去
        url: https://ai.baidu.com/ai-doc/ANTIPORN/Wk3h6xg56
        :param test_image: 待测试的图片 可以本地文件 也可以网址
        :param mode:  默认 = 0 表示 识别的本地文件   mode = 1 表示识别的图片网址连接
        :return: 返回识别结果
        """
        if mode == 0:
            filepath = self.return_image_content(self.return_path(test_image=test_image))
        elif mode == 1:
            filepath = test_image
        else:
            raise Exception("The mode is 0 or 1 but your mode is ", mode)
        # 调用色情识别接口
        result = self.client.imageCensorUserDefined(filepath)

        # """ 如果图片是url调用如下 """
        # result = self.client.imageCensorUserDefined('http://www.example.com/image.jpg')
        print(result)
        return result


a = Auditing()
a.return_Content_by_baidu_of_image("test_image/2.jpg", mode=0)

Learn muggle_ocr to identify the interface:

This package has become popular recently, it is very simple to use, there are not many other functions

  1. Install pip install muggle-ocr This download is a bit slow, it is better to use the mobile hotspot. Currently the mirror website (Tsinghua/Ali) has not been updated to this package because this package is the latest ocr model 12

  2. Call interface

 def return_ocr_by_muggle(self, test_image, mode=1):
        """
            调用这个函数使用 muggle_ocr 来进行识别
            :param  test_image  待测试的文件名称 最好绝对路径
            :param  模型 mode = 0  即 ModelType.OCR 表示识别普通印刷文本
                  当 mode = 1 默认  即 ModelType.Captcha 表示识别4-6位简单英输验证码

            官方网站: https://pypi.org/project/muggle-ocr/
            :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()

Package source code:

# -*- coding :  utf-8 -*-
# @Time      :  2020/10/22  14:12
# @author    :  沙漏在下雨
# @Software  :  PyCharm
# @CSDN      :  https://me.csdn.net/qq_45906219

import muggle_ocr
import os
from aip import AipOcr

"""
    PS: 这个作用主要是作了一个封装 把2个常用的图片/验证码识别方式合在一起 怎么用 取决于自己
    
    接口1: muggle_ocr 
          pip install muggle-ocr 这个下载有点慢 最好使用手机热点
          目前镜像网站(清华/阿里)  还没有更新到这个包 因为这个包是最新的一个ocr模型
          
    接口2: baidu-aip
          pip install baidu-aip
          这个知道的人应该很多很多, 但是我觉得还是muggle 这个新包猛的一比
          调用方式 可以参考官网文档: https://cloud.baidu.com/doc/OCR/index.html
          或者使用我如下的方式  都是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_image
        else:
            filepath = os.path.abspath(test_image)
        return filepath

    def 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)
                    相关参考网址:
                    https://cloud.baidu.com/doc/OCR/s/3k3h7yeqa
        :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 来进行识别
            :param  test_image  待测试的文件名称 最好绝对路径
            :param  模型 mode = 0  即 ModelType.OCR 表示识别普通印刷文本
                  当 mode = 1 默认  即 ModelType.Captcha 表示识别4-6位简单英输验证码

            官方网站: https://pypi.org/project/muggle-ocr/
            :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')

Guess you like

Origin blog.csdn.net/Python_kele/article/details/115014983