Python crawler cloud verification code recognition

Disclaimer: Since the publication of this article, this article is for reference only and may not be reproduced or copied. If the party who browses this article is involved in any violation of national laws and regulations, all consequences shall be borne by the party who browses this article and has nothing to do with the blogger of this article. And due to the reprinting, copying and other operations of the parties who browse this article, any disputes caused by violation of national laws and regulations and all the consequences shall be borne by the parties who browse this article and have nothing to do with the blogger of this article.

1. Baidu Smart Cloud

I use Baidu Smart Cloud , and it is estimated that few people are using the cloud coding platform.
https://cloud.baidu.com/
for login and registration.

Insert picture description hereInsert picture description here

It's free to start, and it's enough for learning OCR text recognition.
Tried three, each has its own advantages and disadvantages.

Insert picture description hereInsert picture description here

2. Use

> **Here is a quote**

Both are used, and the SDK supports multiple languages.

Insert picture description here

2.1 API documentation usage

Insert picture description here

Jump to the connection obtained by Access Token.
Below is the usage method of Python.

Insert picture description here

By official document summarizes a:
The following is ocr.pya file.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import requests
import base64

'''
百度 ocr 提供了模板
验证码识别
'''


# 得到 access_token
def get_access_token(client_id, client_secret):
    # client_id 为官网获取的 AK, client_secret 为官网获取的 SK.
    host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}'
    response = requests.get(host)
    if response:
        print(response.json())
        return response.json()["access_token"]


'''
client_id: API Key
client_secret: Secret Key
url_img: 照片的 URl
type_: 选择识别的方式, 默认为 webimage_loc.
    - general: 通用文字识别(标准含位置版)
    - accurate: 通用文字识别(高精度含位置版)
    - webimage_loc: 网络图片文字识别(含位置版)
'''


# 得到结果
def baidu_OCR(client_id, client_secret, url_img, type_="webimage_loc"):
    request_url = f"https://aip.baidubce.com/rest/2.0/ocr/v1/{type_}"
    # url
    request_url = request_url + "?access_token=" + get_access_token(client_id, client_secret)
    # 图片文件的二进制形式
    b_img = requests.get(url_img).content
    # b_img = open('./data/4.png', 'rb').read()
    img = base64.b64encode(b_img)
    # 参数
    params = {
    
    
        "image": img
    }
    headers = {
    
    
        'content-type': 'application/x-www-form-urlencoded'
    }
    # 请求
    response = requests.post(request_url, data=params, headers=headers)
    # 返回结果
    if response:
        print(response.json())
        return response.json()["words_result"][0]["words"]

Quoting the above method

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import ocr

if __name__ == '__main__':
    # API Key
    client_id = "client_id"
    # Secret Key
    client_secret = "client_secret"
    # 照片的路径
    url_img = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605085260223&di=f3ac8274fc8d8aa5830cfdd0e75e5586&imgtype=0&src=http%3A%2F%2Fimg3.itboth.com%2F11%2F96%2FM3Enmm.jpg"
    # 输出识别后的结果
    # print(baidu_OCR(client_id, client_secret, url_img))
    print(ocr.baidu_OCR(client_id, client_secret, url_img, type_="general"))
    print(ocr.baidu_OCR(client_id, client_secret, url_img, type_="accurate"))
    print(ocr.baidu_OCR(client_id, client_secret, url_img, type_="webimage_loc"))


Insert picture description here

2.2 SDK documentation usage

In PyCharm End mounted directly baidu-aipto.
Have used the following method.

Insert picture description here
Insert picture description here

According to the documentation:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import requests
from aip import AipOcr

'''
调用百度 ocr 接口
验证码识别
'''


# 读取图片
def get_file_content(url_img):
    # 图片文件的二进制形式
    return requests.get(url_img).content


if __name__ == '__main__':

    """ 你的 APPID AK SK """
    APP_ID = 'APP_ID'
    API_KEY = 'API_KEY'
    SECRET_KEY = 'SECRET_KEY'

    # SDK客户端
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

    # 照片连接
    url_img = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605085260223&di=f3ac8274fc8d8aa5830cfdd0e75e5586&imgtype=0&src=http%3A%2F%2Fimg3.itboth.com%2F11%2F96%2FM3Enmm.jpg"

    # 读取图片
    image = get_file_content(url_img)

    # 如果有可选参数
    options = {
    
    
        "recognize_granularity": "big",
        "detect_direction": "true",
        "vertexes_location": "true",
        "probability": "true"
    }

    # 调用通用文字识别(含位置信息版), 图片参数为远程 url 图片
    generalUrl = client.generalUrl(url_img)
    print(generalUrl)
    print(generalUrl["words_result"][0]["words"])
    # 带参数调用通用文字识别(含位置高精度版)
    accurate = client.accurate(image, options)
    print(accurate)
    print(accurate["words_result"][0]["words"])
    # 调用网络图片文字识别, 图片参数为远程 url 图片
    web_image_url = client.webImageUrl(url_img)
    print(web_image_url)
    print(web_image_url["words_result"][0]["words"])

Insert picture description here

3. Get the verification code from Gushiwen.com

Gushiwen website
https://so.gushiwen.cn/user/login.aspx
Here you only get the verification code, log in at the back.

Need to convert gif to png format, need to install pillow

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import requests
from lxml import etree
from aip import AipOcr
from PIL import Image


# gif 转化为 png
def gif_png(img_path, img_name):
    im = Image.open(img_path)

    # gif 转化为 png
    def iter_frames(image):
        try:
            j = 0
            while 1:
                image.seek(j)
                image_frame = image.copy()
                palette = image_frame.getpalette()
                if j > 0:
                    image_frame.putpalette(palette)
                yield image_frame
                j += 1
        except EOFError:
            pass

    for i, frame in enumerate(iter_frames(im)):
        frame.save(img_name, **frame.info)


# 读取文件
def get_file_content(file_path):
    with open(file_path, 'rb') as fp:
        return fp.read()


# 得到验证码
def getCode(url_image):
    # 保存照片
    with open("./data/login.gif", 'wb') as f:
        f.write(requests.get(url_image).content)

    """ 你的 APPID AK SK """
    APP_ID = '22962182'
    API_KEY = 'NhPmiDM9vM5B9SeH9fYM6tty'
    SECRET_KEY = 'nWa2ix6mlqRcec3xzVeHaCzAqWPOS8Fx'

    # SDK客户端
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    # 转化
    gif_png("./data/login.gif", "./data/login.png")
    url_content = get_file_content("./data/login.png")
    # 调用通用文字识别(含位置信息版)
    general_url = client.general(url_content)
    return general_url["words_result"][0]["words"]


if __name__ == '__main__':
    # url, UA
    url = "https://so.gushiwen.cn/user/login.aspx"
    headers = {
    
    
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0"
    }
    # 爬取
    text = requests.get(url=url, headers=headers).text
    # 解析
    tree = etree.HTML(text, etree.HTMLParser(encoding="utf-8"))
    # 得到照片 url
    url_img = "https://so.gushiwen.cn" + tree.xpath("//img[@id='imgCode']/@src")[0]
    # 得到验证码
    login_code = getCode(url_img)
    print(login_code)

The effect is as follows, I personally feel that the accuracy is not too high, but it is also possible for free.

Insert picture description here

Guess you like

Origin blog.csdn.net/YKenan/article/details/111990974