本文由广大附中摸鱼小组创作
作业帮诗词pk题目样例
运行脚本前的准备工作
注意,此脚本可能只支持windows操作系统(其他操作系统的兼容性未经过测试)
1.在电脑上下载夜神模拟器 官网网址为https://www.yeshen.com/
2.运行夜神模拟器,进入夜神模拟器的设置界面,将分辨率修改为“手机版”“900×1600”
3.在搜索引擎搜索“作业帮口算”,并下载作业帮口算的安装包(建议先下载到电脑上,而不是直接下载到模拟器中)
4.在夜神模拟器正在运行的情况下,在电脑上(不是在模拟器中)用鼠标双击作业帮口算的安装包,等待一段时间,即可自动完成作业帮口算的安装(因为我的作业帮口算的安装包已经删了,所以这一步就不发图片了,不过相信大家是可以弄明白的)
5.用**python3.8(其他版本的python兼容性未经过测试)**打开我的脚本(脚本在此文的后半部分)
6.找到夜神模拟器的快捷方式,右键点击,点击右键菜单中的“打开文件所在的位置(I)”选项,打开夜神模拟器的安装目录
7.复制夜神模拟器的安装目录(注意是bin目录)
8.打开脚本(脚本在后面),将脚本中的D:/Program Files/Nox/bin替换成你自己的bin目录(替换完成后,一定要再用python的查找功能查找一下是不是还有D:/Program Files/Nox/bin没成功替换的,避免操作失误造成严重后果)
9.打开夜神模拟器的电脑共享目录
10.打开ImageShare文件夹,复制路径
11.打开脚本,将C:/Users/Administrator/Nox_share/ImageShare目录替换成自己的ImageShare目录(同样,替换完之后要用python的“查找”功能检查是否还有C:/Users/Administrator/Nox_share/ImageShare目录没替换成功)
12.用pip安装ddddocr模块(为加快下载速度,可以考虑换源)
(PIL库我忘记是不是python自带的了,如果不是,可能也要手动安装)
13.请删除腾讯的安装目录下的AndroidServer目录中的所有文件(因为腾讯QQ捆绑安装了一个adb,他会和我们夜神模拟器的adb冲突)(如果提示权限不够,请修改该文件夹的权限,此处不再赘述具体方法)
(比较恶心的事)
14.进入作业帮口算,打开诗词pk
15.运行脚本即可(注意,当脚本进行完一次pk后,脚本会停顿5s-10s,然后会自动点击继续下一局pk的按钮,在这段时间内,有时候脚本会不小心点进作业帮的其他页面,但是你不用担心,过几秒后脚本会自动解决这个问题,所以你完全没有必要自己去去手动点击继续pk的按钮)
16.玄学问题:有时候脚本会出现无法截图并识别题目的情况,这种情况下,请重启模拟器,他大概率就会恢复正常(据我所知,这种情况下是脚本在执行adb的截图命令的时候出错了,报错信息为No devices/emulators found,但是我并没有在网上找到任何正规的解决方案)
脚本编写思路(未写完,下次再补)
在脚本运行过程中,脚本将不断循环用adb命令截图,将图片中题目和选项对应的部分进行裁剪,并用ddddocr识别出裁剪出的图片中的题目和四个选项,并从网络上用爬虫搜索完整的诗句,并判断哪个选项中的字是在诗句中出现过的,然后点击那个选项。当比赛结束后,脚本会停顿一段时间,然后自动点击继续pk的按钮
脚本内容
import os
import subprocess
import ddddocr
from time import sleep
from PIL import Image
import requests
from urllib.parse import quote
import re
import msvcrt
temp="abc"
result="1"
same=0
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
n=0
num=0
def take_screenshot():
process=subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell screencap -p /storage/emulated/0/Pictures/test.png""", startupinfo=startupinfo)
with open('C:/Users/Administrator/Nox_share/ImageShare/test.png', 'rb') as f:
print("正在获取屏幕截图……\n")
return f.read()
def remove_non_chinese(text):
result = re.sub(r'[^\u4e00-\u9fa5]', '', text)
return result
def remove_last(s, target):
index = s.rfind(target)
if index == -1:
return s
return s[:index] + s[index + len(target):]
print("正在初始化ocr……\n")
ocr = ddddocr.DdddOcr(show_ad=False)
while True:
#sleep(0.5)
num+=1
screenshot=take_screenshot()
if os.path.getsize('C:/Users/Administrator/Nox_share/ImageShare/test.png') > 0:
try:
with Image.open("C:/Users/Administrator/Nox_share/ImageShare/test.png") as img:
img_question1=img.crop((98,567,790,669))
img_question1.save("question1.png")
img_question2=img.crop((101,705,797,800))
img_question2.save("question2.png")
img_choice1=img.crop((186,1100,300,1191))
img_choice1.save("choice1.png")
img_choice2=img.crop((601,1095,720,1192))
img_choice2.save("choice2.png")
img_choice3=img.crop((186,1336,305,1440))
img_choice3.save("choice3.png")
img_choice4=img.crop((596,1335,714,1441))
img_choice4.save("choice4.png")
img_finishbutton=img.crop((159,1348,349,1400))
img_finishbutton.save("finish.png")
img_finish2=img.crop((262,78,326,113))
img_finish2.save("finish2.png")
img_finish3=img.crop((403,1513,499,1566))
img_finish3.save("finish3.png")
img_finish4=img.crop((297,391,412,425))
img_finish4.save("finish4.png")
#img_choice1=img.crop(())
image = open("question1.png", "rb").read()
question1 = ocr.classification(image)
question1=remove_non_chinese(question1)
if question1==temp:
same+=1
n+=1
n=n%4
if 1==1:
print(f"识别题目出现重复,正在重试,已重试次数为为{same}\n")
temp=question1
sleep(0.3)
screenshot=take_screenshot()
with Image.open("C:/Users/Administrator/Nox_share/ImageShare/test.png") as img:
img_question1=img.crop((98,567,790,669))
img_question1.save("question1.png")
image = open("question1.png", "rb").read()
question1 = ocr.classification(image)
question1=remove_non_chinese(question1)
img_question2=img.crop((101,705,797,800))
img_question2.save("question2.png")
img_choice1=img.crop((186,1100,300,1191))
img_choice1.save("choice1.png")
img_choice2=img.crop((601,1095,720,1192))
img_choice2.save("choice2.png")
img_choice3=img.crop((186,1336,305,1440))
img_choice3.save("choice3.png")
img_choice4=img.crop((596,1335,714,1441))
img_choice4.save("choice4.png")
img_finishbutton=img.crop((159,1348,349,1400))
img_finishbutton.save("finish.png")
img_finish2=img.crop((262,78,326,113))
img_finish2.save("finish2.png")
img_finish3=img.crop((403,1513,499,1566))
img_finish3.save("finish3.png")
img_finish4=img.crop((297,391,412,425))
img_finish4.save("finish4.png")
#temp="abc"
'''
else:
print(f"识别题目出现重复,开启顺序点击模式,same值为{same}\n")
if n==0:
subprocess.Popen("D:/Program Files/Nox/bin/nox_adb.exe shell input tap 239 1145", startupinfo=startupinfo)
if n==1:
subprocess.Popen("D:/Program Files/Nox/bin/nox_adb.exe shell input tap 657 1139", startupinfo=startupinfo)
if n==2:
subprocess.Popen("D:/Program Files/Nox/bin/nox_adb.exe shell input tap 238 1385", startupinfo=startupinfo)
if n==3:
subprocess.Popen("D:/Program Files/Nox/bin/nox_adb.exe shell input tap 661 1385", startupinfo=startupinfo)
'''
if question1!=temp or same>2:
if question1!=temp:
same=0
image = open("question1.png", "rb").read()
question1 = ocr.classification(image)
question1=remove_non_chinese(question1)
temp=question1
image = open("finish.png", "rb").read()
finish = ocr.classification(image)
finish=remove_non_chinese(finish)
#print(f"finish的值为{finish}\n")
if(finish=="炫耀一下" or finish=="屹耀下" or finish=="灯烟丁"):
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 643 1380""", startupinfo=startupinfo)
sleep(5)
image = open("finish2.png", "rb").read()
finish2 = ocr.classification(image)
finish2=remove_non_chinese(finish2)
if finish2=="原文":
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 48 96""", startupinfo=startupinfo)
sleep(1)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 672 1504""", startupinfo=startupinfo)
sleep(1)
image = open("finish3.png", "rb").read()
finish3 = ocr.classification(image)
finish3=remove_non_chinese(finish3)
if finish3=="取消":
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 448 1538""", startupinfo=startupinfo)
sleep(1)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 643 1380""", startupinfo=startupinfo)
sleep(5)
image = open("finish4.png", "rb").read()
finish4 = ocr.classification(image)
finish4=remove_non_chinese(finish4)
print(f"finish4的值为{finish4}\n")
if finish4=="暂无内容":
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input keyevent KEYCODE_BACK""", startupinfo=startupinfo)
sleep(1)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 672 1504""", startupinfo=startupinfo)
sleep(5)
print("题目第一行:",question1,"\n")
image = open("question2.png", "rb").read()
question2 = ocr.classification(image)
question2=remove_non_chinese(question2)
print("题目第二行:",question2,"\n")
image = open("choice1.png", "rb").read()
choice1 = ocr.classification(image)
choice1=remove_non_chinese(choice1)
if(len(choice1)>1):
choice1=choice1[0]
image = open("choice2.png", "rb").read()
choice2 = ocr.classification(image)
choice2=remove_non_chinese(choice2)
if(len(choice2)>1):
choice2=choice2[0]
image = open("choice3.png", "rb").read()
choice3 = ocr.classification(image)
choice3=remove_non_chinese(choice3)
if(len(choice3)>1):
choice3=choice3[0]
image = open("choice4.png", "rb").read()
choice4 = ocr.classification(image)
choice4=remove_non_chinese(choice4)
if(len(choice4)>1):
choice4=choice4[0]
print("识别出的四个选项为:",choice1," ",choice2," ",choice3," ",choice4,"\n")
url = "https://www.gushiwen.cn/search.aspx"
params = {
"value": f"{question1}",
"valuej": "a"
}
params['value']=quote(question1,safe='')
params['valuej']=quote(question1[0],safe='')
headers = {
"Host": "www.gushiwen.cn",
"Cookie": "",
"Sec-Ch-Ua": "",
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.5790.110 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-User": "?1",
"Sec-Fetch-Dest": "document",
"Referer": "https://www.gushiwen.cn/search.aspx?type=shiwen&page=1&value=%e5%8c%97%e9%a3%8e%e5%90%b9%e9%9b%81%e9%9b%aa%e7%ba%b7%e7%ba%b7&valuej=%e5%8c%97",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9"
}
response = requests.get(url, params=params, headers=headers)
response1=remove_non_chinese(response.text)
response1=response1.replace(question1,'',1)
response1=remove_last(response1,question1)
print("正在查询题目中古诗词","\n")
position = response1.find(question1)
if len(question1)==7 or len(question2)==7:
print("识别为七言诗\n")
length=7
if len(question1)==5 or len(question2)==5:
print("识别为五言诗\n")
length=5
if position != -1:
result=response1[position:position+2*length]
print("成功找到诗句:", result,"\n")
else:
params['value']=quote(question2,safe='')
params['valuej']=quote(question2[0],safe='')
response = requests.get(url, params=params, headers=headers)
response2=remove_non_chinese(response.text)
response2=response2.replace(question2,'',1)
response2=remove_last(response2,question2)
position = response2.find(question2)
if position != -1:
result=response2[position-length:position+length]
print("成功找到诗句:",result ,"\n")
else:
print("检索诗句失败,即将开始点击全部选项\n")
#os.system("pause")
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 239 1145""", startupinfo=startupinfo)
sleep(0.3)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 657 1139""", startupinfo=startupinfo)
sleep(0.3)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 238 1385""", startupinfo=startupinfo)
sleep(0.3)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 661 1385""", startupinfo=startupinfo)
continue
already=False
if result.find(choice1)!=-1:
print("答案为选项A\n")
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 239 1145""", startupinfo=startupinfo)
already=True
if result.find(choice2)!=-1:
print("答案为选项B\n")
if(already==True):
sleep(0.3)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 657 1139""", startupinfo=startupinfo)
already=True
if result.find(choice3)!=-1:
print("答案为选项C\n")
if(already==True):
sleep(0.3)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 238 1385""", startupinfo=startupinfo)
already=True
if result.find(choice4)!=-1:
print("答案为选项D\n")
if(already==True):
sleep(0.3)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 661 1385""", startupinfo=startupinfo)
already=True
if result.find(choice1)==-1 and result.find(choice2)==-1 and result.find(choice3)==-1 and result.find(choice4)==-1:
print("无法找到正确的选项,即将开始点击全部选项\n")
#os.system("pause")
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 239 1145""", startupinfo=startupinfo)
sleep(0.3)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 657 1139""", startupinfo=startupinfo)
sleep(0.3)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 238 1385""", startupinfo=startupinfo)
sleep(0.3)
subprocess.Popen(r""""D:/Program Files/Nox/bin/nox_adb.exe" shell input tap 661 1385""", startupinfo=startupinfo)
except Exception as e:
print("发生错误:", e,"\n")
else:
print("截图文件为空!。\n")
备注
有人可能会想到,作业帮诗词pk也可以用鼠标连点器把四个选项都点一下来实现快速做题,但是本人已经向作业帮口算官方反映过有人用鼠标连点器开挂的问题并给他们提供了解决方案,不过作业帮口算截至发稿为止仍未着手解决这个问题。