在某一天赶课比较着急,奈何找不到人可以帮我,于是自己动手写了一个简单的脚本来学习

准备工作:

1、需要一个你不用的手机,打开手机的adb,因为需要用到adb来操作手机

2、进入百度智能云,申请一个 通用的文字识别带位置的,新用户好像是送1000次,对我来说够用了

python编写adb脚本 adb python_python编写adb脚本


3、电脑配置好adb的环境关于python和adb的版本:

python编写adb脚本 adb python_json_02

关于百度智能云的调用,百度给出了一个详细的指导

下面是自己写的一个小脚本,仅供参考学习

我能力有限,有很多细节没有处理好,比如,有的时候一个章节会学习两遍,但对于我来说不耽误事就行。可以自己修改完善
大佬轻喷,感谢

# encoding:utf-8
import sys
import os
import copy
import requests 
import base64
import time
import json
import re

class xuexitong:
    def PngGrab(): #抓取图片
        os.system("adb exec-out screencap -p > C:\\Users\\Administrator\\Desktop\\photo\\hhh.png")
        print("抓取图片成功")
        time.sleep(2)

    def ReturnKek():#返回键
        os.system("adb shell input keyevent 4") 
        print("按下返回键")
        time.sleep(2)

    def ClickPlay():#点击屏幕中间进行播放
        os.system("adb shell input tap 402 527")
        print("点击屏幕播放")
        time.sleep(2)

    def SlideScreen():#向下滑动
        os.system("adb shell input swipe 500 900 500 850")
        print("向下滑动")
    
    def SleepTimes(usertime):#等待时间
        waittime = usertime*60
        time.sleep(waittime)
    
    def ClickButton(p_top,p_left,p_width,p_height): #点击获取到的坐标
        p_x = int(p_left + (p_width - p_width/3))
        p_y = int(p_top + p_height/2)
        strposition = "adb shell input tap "+str(p_x)+" "+str(p_y)
        os.system(strposition)
        time.sleep(8)

    def AnalysisText():#对解析的字符串进行处理
        '''
        0,为列表界面
        1,为播放界面
        2,为没有内容
        '''
        flag = -1
        m_PngAnalysisStr = xuexitong.PngAnalysis()
        if(m_PngAnalysisStr.find("高清")!=-1):
            flag = 1
            print("当前模式为播放视频模式")
        else:
            flag = 0
            print("当前模式为界面模式")
        if(m_PngAnalysisStr.find("未编辑内容") > 0):
            flag = 2
            print("该小章节没有内容")
        

        if(m_PngAnalysisStr == "false"):
            print("解析错误,直接返回")
            exit(0)
        if(flag==-1):
            print("不清楚此刻的状态,返回")
            exit(0)
        if(flag==1):
            json_str = m_PngAnalysisStr.replace("'",'"')
            result = json.loads(json_str)
            num = result['words_result_num']
            for i in range(0,num):
                str_search = result['words_result'][i]['words']
                regex = re.compile('[0-9][0-9]:[0-9][0-9]/[0-9][0-9]:[0-9][0-9]')
                if(regex.findall(str_search)):
                    str_search = str_search[6:8]
                    sleeptimenum = int(str_search)+1
                    print("解析成功,请等待时间:",sleeptimenum)
                    xuexitong.SleepTimes(sleeptimenum)
                    print("播放结束")
                    return 1
        if(flag==0):
            json_str = m_PngAnalysisStr.replace("'",'"')
            result = json.loads(json_str)
            num = result['words_result_num']
            for i in range(7,num):
                str_search = result['words_result'][i]['words']
                regex = re.compile('[0-9]\.[0-9]\w*')
                if(regex.findall(str_search)):
                    if(str_search.find("本章测验")==-1):
                        print("找到需要学习的章节:",str_search)
                        #def ClickButton(p_top,p_left,p_width,p_height): #点击获取到的坐标
                        m_top = result['words_result'][i]['location']['top']
                        m_left = result['words_result'][i]['location']['left']
                        m_width = result['words_result'][i]['location']['width']
                        m_height = result['words_result'][i]['location']['height']
                        xuexitong.ClickButton(m_top,m_left,m_width,m_height)
                        break
        if(flag==2):
            return 2
        
    
    def PngAnalysis():#解析当前的图片
        request_url = "百度api的网址"
        # 二进制方式打开图片文件
        imgPath = "C:\\Users\\Administrator\\Desktop\\photo\\hhh.png"
        f = open(imgPath,'rb')
        img = base64.b64encode(f.read())
        params = {"image":img}
        access_token = '这里是你的access_token'
        request_url = request_url + "?access_token=" + access_token
        headers = {'content-type': '百度中有详细说明'}
        response = requests.post(request_url, data=params, headers=headers)
        if response:
            return response.text
        else:
            return "false"
        
if __name__=='__main__':
    '''
    1、正常播放看完
    2、里面没有内容
    '''
    obj1 = xuexitong
    while(1):
        time.sleep(2)
        print("开始抓取界面列表")
        xuexitong.PngGrab()
        print("开始分析界面")
        time.sleep(5)
        xuexitong.AnalysisText()
        xuexitong.ClickPlay()  #点击播放
        print("判断是否有内容")
        time.sleep(5)
        xuexitong.ClickPlay()  #点击显示
        xuexitong.PngGrab()
        implementResult = xuexitong.AnalysisText()
        if(implementResult == 1):
            xuexitong.ReturnKek()
            xuexitong.ReturnKek()
        if(implementResult == 2):
            xuexitong.ReturnKek()
        xuexitong.SlideScreen()