1 from aip import AipOcr 2 import re 3 #现在百度AI官网申请人工智能接口信息 4 APP_ID="18978400" 5 API_KEY="HEPH99erMT9mb8GqAMZTpHVU" 6 SECRET_KEY="GXMiaoGQzam80GZMd0xtzQhMTEB4aHnr" 7 client=AipOcr(APP_ID,API_KEY,SECRET_KEY) 8 9 with open(r"D:软件pythonpython_workPython_day19123.jpg","rb") as f: 10 11 image=f.read() 12 13 data=str(client.basicGeneral(image)).replace(" ","") 14 # print(data) 15 pat=re.compile(r"{'words':'(.*?')}") #得到一个json格式的内容,用正则匹配想要的信息 16 res=pat.findall(data)[0] 17 print(res)
验证码识别
1 import requests 2 import re 3 from aip import AipOcr 4 APP_ID="18978400" 5 API_KEY="HEPH99erMT9mb8GqAMZTpHVU" 6 SECRET_KEY="GXMiaoGQzam80GZMd0xtzQhMTEB4aHnr" 7 client=AipOcr(APP_ID,API_KEY,SECRET_KEY) 8 #从网页中获取到验证码图片 9 data=requests.get("http://127.0.0.1:8020/验证码登录/login.html").text 10 pat=re.compile(r'<img src="(.*?)" style') 11 url="http://127.0.0.1:8020/验证码登录/"+pat.findall(data)[0] #获取图片所在链接 12 image=requests.get(url).content #获取图片信息,二进制 13 #提取文字内容 14 data1=str(client.basicGeneral(image)).replace(" ","") 15 # print(data) 16 pat1=re.compile(r"{'words':'(.*?')}") #得到一个json格式的内容,用正则匹配想要的信息 17 res=pat1.findall(data1)[0] 18 print(res)