Python 利用OCR 识别图形验证码

本文介绍了在Python3.6中如何利用OCR技术识别图形验证码。通过接口文档,详细讲解了Python2.7与Python3.6版本在处理URL编码、图片转base64、字节转换等方面的差异,并给出了一段具体的代码实现,包括获取验证码图片、保存本地、调用腾讯API识别及获取识别结果的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用的API 首先查看下接口文档

 

一、首先是python2.7版本代码

#encoding=utf-8
import requests
import  time
import  string
import random
import base64
import hashlib
import  urllib
from urllib import urlencode
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)




CommonUrl="https://api.ai.qq.com/fcgi-bin/ocr/ocr_generalocr"
app_id="填自己的"
AppKey="填自己的"
time_stamp=int(time.time())
nonce_str=''.join(random.sample(string.ascii_letters + string.digits, 15))


def MakeSign():
    Dict ={'app_id': app_id, 'time_stamp': time_stamp, 'nonce_str': nonce_str, 'image': ImageBase64()}
    T_str1 = ""
    for key in sorted(Dict.keys()):
        if key == 'app_key':
            continue
        T_str1 = T_str1 + "%s=%s&" % (key, urllib.quote(str(Dict[key]), safe=''))
    T_sgin = T_str1 + 'app_key=' + AppKey
    signraw=T_sgin.encode("utf-8")
    hash_md5 = hashlib.md5()
    hash_md5.update(signraw)
    sign=hash_md5.hexdigest().upper()
    return sign



def ImageBase64():
    imagebase64=None
    try:
        with open("C:\\Users\\Administrator\\Desktop\\xiejiangpeng.pn1g", "rb") as f:
            imagebase64 = base64.b64encode(f.read())
    except Exception as ex:
        print(ex)
        exit()
    return imagebase64




sign=MakeSign()#签名
image=ImageBase64()#图片


#调用OCR图片识别接口
TengXunAiCommonBody={'app_id':app_id,'time_stamp':time_stamp,'nonce_str':nonce_str,'sign':sign,'image':image}
TengXunAiCommon=requests.post(CommonUrl,TengXunAiCommonBody,verify=False)
try:
    temp=TengXunAiCommon.json()["data"]["item_list"][0][
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值