思路:
1.使用pil库对gif逐帧保存
2.使用ddddocr全部识别
3.看下验证码多少位 if判断一下识别的长度等于验证码长度就保存到list里
4.获取list出现最多次数的验证码
代码实现:
# -*- coding: utf-8 -*-
"""
@Time : 2022/2/10 17:20
@Auth : 黑脸怪
@File :1.py
"""
import requests
import ddddocr
from PIL import Image
dddd = ddddocr.DdddOcr()
#下载gif验证码
headers={
"Cookie": ""
}
img_response=requests.get("http://credit.customs.gov.cn/ccppserver/verifyCode/creator",headers=headers)
with open("./test.gif","wb+") as f:
f.write(img_response.content)
#逐帧保存验证码到gif文件夹(需要先创建)
frame=Image.open("./test.gif")
nframes=0
while frame:
frame.save("./gif/test"+str(nframes)+".png")
nframes+=1
try:
frame.seek(nframes)
except EOFError:
break
#处理验证码
def yzm_handler():
mylist=[]
#因为gif验证码一共24张图 循环识别
for a in range(0,25):
with open(f"./gif/test{str(a)}.png", "rb") as f:
img = f.read()
res = dddd.classification(img)
# print(res)
if len(res)==4:
mylist.append(res)
print(mylist)
return max(mylist, key=mylist.count)
res=yzm_handler()
print("验证码是:",res)
评论 (0)