Python使用完美验证码识别系统笔记

背景

Python版本:3.6.4 32bit,必须是32位,不然调用不了WmCode.dll
http库:Requests
Windows10

注意事项

  1. UseUnicodeString接口需要放到LoadLibrary之后,且在LoadWmFromFileEx之前;
  2. SetWmOptionEx需要放到LoadWmFromFileEx之后;
  3. 需要使用下面代码截取出有效的验证码:
def ocrVCodeByBuffer(index, buffer, length):
    '''使用字库识别验证码'''
    print("OCR by {index}".format(index=index))
    Str = create_string_buffer(10)
    if(dll.GetImageFromBufferEx(index, ctypes.pointer(buffer), length, Str)):
        vc1 = str(repr(Str.raw)).split('\\x')[0][2:] #这句
        return vc1
    else:
        print('GetVcode Fail!')
        return None
  1. 数据转换
from ctypes import *

import requests
import ctypes

def getRandomImageBuffer(cookie):
    '''    get vcode buffer    '''
    if cookie is False:
        return False
    headers = {
        "Referer": "https://xx.cn/index.html",
        "User-Agent": "Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0",
        'Accept': 'image/apng',
        'Accept-Encoding': 'gzip, deflate, br',
        'Cache-Control': 'no-cache'
    }
    url = 'https://xx.cn/getRandomImage.php'
    r = requests.get(url, headers=headers, cookies=cookie, stream=True, timeout=30.0)
    if r.status_code is not 200:
        return False
    content_length = len(r.content)
    content = ctypes.c_char * content_length
    c = content.from_buffer_copy(r.content)
    return True, c, content_length

你可能感兴趣的:(Windows/.net,core,Python/Django)