python控制手机自动刷新闻_python +adb控制手机自动化签到
#coding=utf8
importos,re,time,loggingimportpyautoguifrom apscheduler.schedulers.background import BackgroundScheduler #后臺運行任務計劃
sc=BackgroundScheduler()
UIFILE= "ui.xml"
#如何找APP的值,請看runapp()函數注釋
APP = r"com.XXX/XXXActivity"logging.basicConfig(filename="adbwork.log",filemode="w",format="%(asctime)s-%(name)s-%(levelname)s-%(message)s",level=logging.INFO)#執行cmd命令
defa(c):
r=os.popen(c).read()returnr#判斷設備是否連接
defcaniuse():
s= a("adb devices").split("\n")if s[1]==‘‘:returnFalseelse:returnTrue#判斷wifi是否連接#adb shell getprop wifi.interface 得到返回wlan0
defcanwifi():
s= a("adb shell getprop dhcp.wlan0.result").split("\n")if s[0]==‘ok‘:returnTrueelse:returnFalse#清理關閉所有程序==可能不適用您的手機
defclean():
a("adb shell input keyevent 187") #切換應用
time.sleep(1)
a("adb shell input tap 540 1740") #點擊清掃按鈕
time.sleep(1)#判斷手機電源是否打開
defpoweron():
s= a("adb shell dumpsys power")
s= re.search(r"Display Power: state=ON",s,re.I |re.M)if s ==None:returnFalseelse:returnTrue#啟動APP
defrunapp(app):#獲取所有package:adb shell pm list packages 查看所有包名
#獲取當前package和activity:首先打開APP,然后運行:adb shell dumpsys window | findstr mCurrentFocus
#獲取package和activity:1、首先運行:adb shell dumpsys package 用上一行命令獲取的package名
#2、然后在輸入結果中找 Activity Resolver Table: ... Non-Data Actions:下面第2行,
#類似1960b60b com.baidu.searchbox/com.baidu.android.pushservice.PushPatchMessageReceiver的
#用作本函數的參數,以啟動APP
a("adb shell am start"+app)def Tap(kw,ordinal=0):
ui()#刷新
while notos.path.isfile(UIFILE):
time.sleep(0.5)
with open(UIFILE, mode=‘r‘, encoding=‘UTF-8‘) as f: #打開文件
data = f.read() #讀取文件
regkw = "]+?" + kw + ".[^>]+?\[(\d+),(\d+)\]\[(\d+),(\d+)\].+?>"a=re.findall(regkw, data)
x1=int(a[ordinal][0])
y1= int(a[ordinal][1])
x2= int(a[ordinal][2])
y2= int(a[ordinal][3])
xx= int((x2-x1)/2 +x1)
yy= int((y2-y1)/2 +y1)
cmd= "adb shell input tap" + str(xx) +" " +str(yy)
r=os.popen(cmd).read()return ‘‘
#獲取手機當前頁面的源代碼,并復制到當前腳本所在目錄
defui():
a(r"cmd /c del ui.xml -y")#刪除當前目錄下舊的ui.xml(如果有的話)
a("adb shell rm /sdcard/ui.xml")#刪除手機中舊的的ui.xml(如果有的話)
a("adb shell uiautomator dump /sdcard/ui.xml") #重新dump
time.sleep(0)#等待保存到手機卡
a("adb pull /sdcard/ui.xml .")
time.sleep(0)#等待下載到電腦
#判斷用ui()函數獲取的ui.xml的源碼中是否包括1個或多個關鍵字(不能用正則表達式)#用法:uiContains("關鍵字1","關鍵字n")
def uiContains(*kw):
numkw=len(kw)
kws=‘‘
if numkw<1:returnFalsefor i inrange(len(kw)):
kws= kws + kw[i]+‘|‘kws= kws.strip(‘|‘)
ui()#刷新
while notos.path.isfile(UIFILE):
time.sleep(0.5)
with open(UIFILE, mode=‘r‘, encoding=‘UTF-8‘) as f: #打開文件
data = f.read() #讀取文件
a =re.findall(kws, data)
numkwfind=len(set(a))if numkw ==numkwfind:returnTrueelse:returnFalse#判斷用ui()函數獲取的ui.xml的源碼中是否匹配1個“正則表達式”#用法:uiRegEx("正則表達式")
defuiRegEx(regkw):
ui()#刷新
while notos.path.isfile(UIFILE):
time.sleep(0.5)
with open(UIFILE, mode=‘r‘, encoding=‘UTF-8‘) as f: #打開文件
data = f.read() #讀取文件
a =re.findall(regkw, data)print(a)
num=len(a)print(num)if num >0 :returnTrueelse:returnFalse#強制退出應用adb shell am force-stop #a("adb shell am force-stop XXXX")#點擊:adb shell input tap 939 1820#滑動:adb shell input swipe 451 1183 490 321#長按:將滑動距離縮小并加延時1秒 adb shell input swipe 500 1500 500 1500 1000#模擬按鍵:adb shell input keyevent 26#常用keycode#HOME鍵3 返回鍵4 電源鍵26#切換應用187 點亮屏幕224 系統休眠223#KEYCODE_0表示數字0 KEYCODE_ENTER KEYCODE_TAB
#簽到,要根據個人手機情況微調
defqd():
a("adb shell input tap 939 1820") #點積分
time.sleep(1)#簽到積分
@sc.scheduled_job(‘interval‘, seconds=3600*8 ,id=‘dowork‘) #每隔X秒執行一次
defdowork():#開始操作
logging.info("開始簽到")
a("adb shell input keyevent 224") #點亮手機屏幕
time.sleep(1)
a("adb shell input swipe 500 1500 500 400")#向上滑動
time.sleep(1)
a("adb shell input keyevent 3") #HOME
if caniuse() == False:#如果在手機點亮前檢查,經常是連接不上
pyautogui.alert("請檢查手機連接狀態")return
if canwifi() == False:#如果在手機點亮前檢查,經常是連接不上
pyautogui.alert("請檢查wifi狀態")returnrunapp(APP)#啟動應用
time.sleep(30)#手機休眠前清理應用==================================
clean()
a("adb shell input keyevent 223") #系統休眠
logging.info("完成簽到")if __name__ == ‘__main__‘:
dowork()
sc.start()while(1):#保持程序不退出
pass
總結
以上是生活随笔為你收集整理的python控制手机自动刷新闻_python +adb控制手机自动化签到的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电话手表屏幕碎了怎么办
- 下一篇: python完整安装顺序_Python安