准备工作:
python3
申请一个百度OCR的appid、appkey、secretkey,不会申请 开通的戳下面链接,视频中有详细说明:
整体思路:
- 1、设置固定微信PC端浏览器位置及窗口大小,以便确定截图坐标范围以及获取各个指定坐标颜色信息;
- 2、确定好截图坐标范围后截图,通过百度文字识别OCR接口上传,进行文字识别,获取题目内容;
- 3、在本地题库搜索该题目,如果没有答案就随机点击前两个答案;
- 4、通过获取点击答案后固定位置出现的✔标志颜色,来判断哪个选项为正确答案;
- 5、获取到正确答案选项后,将题目与答案写入本地题库;
- 6、如果在第3步在本地题库中找到答案,那么就点击答案选项;
- 7、循环控制,通过获取指定位置颜色确定是否开始答题或进入下一局。
完整代码:
import jsonpath
from aip import AipOcr
import pyautogui as ptg
from io import BytesIO
import time
import openpyxl
import os
import random
import win32gui
import win32con
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
path='./answer.xlsx'
zanswer=[]
if not os.path.exists(path):
workbook = openpyxl.Workbook()
workbook.save(path)
else:
data = openpyxl.load_workbook(path)
table = data.active
nrows = table.max_row
for i in range(1,nrows+1):
tmp = []
tmp.append(table.cell(i, 1).value)
tmp.append(table.cell(i, 2).value)
zanswer.append(tmp)
data.close()
def uppic(img):
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 识别参数
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "false"
options["detect_language"] = "false"
options["probability"] = "false"
# 图片识别
result = client.basicGeneral(img, options)
return result
def wxls(qList):
w=True
for i in zanswer:
if qList[0]==i[0]:
w=False
print(qList[0],'已存在')
break
if w==True:
print('题库记录:%d,%s'%(len(zanswer),qList))
data = openpyxl.load_workbook(path)
table = data.active
nrows = table.max_row
for j in range(1, 3):
table.cell(nrows+1, j).value = qList[j-1]
data.save(path)
zanswer.append(qList)
def sshot(a,b,c,d):
im=ptg.screenshot(region=(a,b,c,d))
buffered = BytesIO()
im.save(buffered, format="JPEG")
img_Bytes = buffered.getvalue()
return uppic(img_Bytes)
def rgbPro(s):
strCx = s[-2:]
if len(s) == 3:
strCx = '0' + s[2]
return strCx
def getColor(color):
a1 = hex(color[0]) #R
a2 = hex(color[1]) #G
a3 = hex(color[2]) #B
s1 = rgbPro(str(a1))
s2 = rgbPro(str(a2))
s3 = rgbPro(str(a3))
strC = str.upper(s3)+ str.upper(s2) + str.upper(s1)
return strC #BGR
def leftClick(x,y):
a, b = ptg.position()
ptg.click(x,y)
time.sleep(0.1)
ptg.moveTo(a,b)
def main():
quitFlag=0
while True:
color = getColor(ptg.pixel(150, 90))
if (color != '5448DC') and (color != '37308A') and (color !='1D1A3F'): # 判断是否退出
if quitFlag > 1: exit()
quitFlag+=1
time.sleep(1)
else:
color = getColor(ptg.pixel(265,768)) # 判断再来一次
if color == '6A4DFF':
leftClick(265,768)
time.sleep(6)
continue
quitFlag=0
try:
color = getColor(ptg.pixel(84, 845)) # 判断再来一次
if color == 'FFAA00':
leftClick(84+random.randint(0,20), 845+random.randint(0,10))
time.sleep(4)
color = getColor(ptg.pixel(250, 660)) # 判断开始答题
if color == '00FFFF':
leftClick(250, 660)
time.sleep(4)
#题目区域截图上传OCR识别
rjson = sshot(6, 285, 487, 223) #获取题目
rnum = rjson['words_result_num']
if int(rnum) == 0: continue
rstr = jsonpath.jsonpath(rjson, '$..words')
# 拼接题目
tm =''
for i in range(0,len(rstr)):
tm=tm+rstr[i]
#特殊字符过滤
repstr=',.",、。“”[]【】〈〉<>《》?_ '
for i in repstr:
tm=tm.replace(i,'')
#题库查找是否存在
w=False
for i in zanswer:
if tm==i[0]:
#如果存在就点击对应位置
print('找到答案:题目:%s,答案:%s'%(i[0],i[1]))
# x=75+random.randint(0,320)
# y=560+random.randint(0,30)+(int(i[1])*103)
leftClick(75,560+(int(i[1])*103))
time.sleep(4)
#给一个找到答案的Flag
w=True
break
if w==True:
#找到答案就进入下一轮
time.sleep(4)
continue
else:
#没找到答案
#答案最多4个:0,1,2,3
answer=-1
leftClick(75,560+(random.randint(0,1)*103))
time.sleep(0.5)
try:
for i in range(0,4):
color = getColor(ptg.pixel(404, 558+(i*104)))
if color == '3EC68C': #答案正确
answer=i
break
if answer == -1: continue
except:
continue
wzanswer=[]
wzanswer.append(tm)
wzanswer.append(answer)
#写入EXCEL
wxls(wzanswer)
time.sleep(4)
#time.sleep(random.randint(5,10))
except:
pass
if __name__ == '__main__':
hwnd=win32gui.FindWindow('CefWebViewWnd','微信')
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0,0, 500,1000, win32con.SWP_SHOWWINDOW)
main()
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习使用,请在下载后24小时内删除,严禁商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
【注意:本站发布资源来源于网络搜集,均有较强时效性,请在下载前注意查看文章资源发布或更新时间,距离当前时间太久的资源不建议下载,特别是安卓专区相关资源,会有大概率失效无法使用】
评论(0)