代码实现
from appium import webdriver
from selenium.webdriver.common.by import By
import time
import subprocess
import random
def kuaishou(adb_name):
adb_path = r"D:\adb\adb.exe"
desired_caps = {
'platformName': 'Android',
'platformVersion': '7',
'deviceName': 'xxx',
'appPackage': 'com.kuaishou.nebula',
'appActivity': 'com.yxcorp.gifshow.HomeActivity',
'unicodeKeyboard': True,
'resetKeyboard': True,
'noReset': True,
'newCommandTimeout': 6000,
'automationName' : 'UiAutomator2'
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
num = 0
elapsed = 0
judge = 0
box = 1
while True:
try:
driver.implicitly_wait(40)
Comment_icon = driver.find_elements(By.ID, "com.kuaishou.nebula:id/comment_icon")
print(bool(Comment_icon),'出现评论图标')
if bool(Comment_icon) == False:
subprocess.getoutput(f"{adb_path} -s {adb_name} shell input keyevent 4")
continue
start = time.time()
subprocess.getoutput(f"{adb_path} -s {adb_name} shell input swipe 360 1000 360 120")
sleep_time = random.randint(7, 12)
time.sleep(sleep_time)
num += 1
print('观看第{}个视频,观看{}秒'.format(num, sleep_time))
end = time.time()
elapsed += int(end - start)
Comment_icon = driver.find_elements(By.ID, "com.kuaishou.nebula:id/comment_icon")
if bool(Comment_icon) == False:
print(bool(Comment_icon), '是直播,图片或其他异常,过滤掉')
continue
if judge <= elapsed:
print('宝箱的时间到了')
if Comment_icon:
subprocess.getoutput(f"{adb_path} -s {adb_name} shell input tap 68 750")
tasks = driver.find_elements(By.XPATH,
"/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.widget.ListView/android.view.View[2]/android.view.View[1]/android.widget.TextView")
if tasks:
print('进入任务页面')
time.sleep(2)
subprocess.getoutput(f"{adb_path} -s {adb_name} shell input tap 604 1155")
time.sleep(2)
subprocess.getoutput(f"{adb_path} -s {adb_name} shell input keyevent 4")
print(f'宝箱已领{box}个')
judge += 1200
box += 1
if (bool(Comment_icon) == False) and (bool(tasks) == False):
subprocess.getoutput(f"{adb_path} -s {adb_name} shell input keyevent 4")
except:
print('出错了,重启')
subprocess.getoutput(f"{adb_path} -s {adb_name} shell am force-stop com.kuaishou.nebula")
kuaishou(adb_name)
def RW(adb_name, num=1):
adb_path = r"D:\adb\adb.exe"
desired_caps = {
'platformName': 'Android',
'platformVersion': '7',
'deviceName': 'xxx',
'appPackage': 'com.kuaishou.nebula',
'appActivity': 'com.yxcorp.gifshow.HomeActivity',
'unicodeKeyboard': True,
'resetKeyboard': True,
'noReset': True,
'newCommandTimeout': 6000,
'automationName' : 'UiAutomator2'
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
while True:
try:
driver.implicitly_wait(40)
Comment_icon = driver.find_elements(By.ID, "com.kuaishou.nebula:id/comment_icon")
print(bool(Comment_icon),'出现评论图标')
if bool(Comment_icon) == False:
subprocess.getoutput(f"{adb_path} -s {adb_name} shell input keyevent 4")
continue
subprocess.getoutput(f"{adb_path} -s {adb_name} shell input tap 68 750")
tasks = driver.find_elements(By.XPATH,
"/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.widget.ListView/android.view.View[2]/android.view.View[1]/android.widget.TextView")
if tasks:
time.sleep(3)
for i in range(10):
subprocess.getoutput(f"{adb_path} -s {adb_name} shell input swipe 352 1077 352 777")
welfare = driver.find_elements(By.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[4]/android.view.View[1]")
if welfare:
break
else:
continue
for i in range(12-num):
welfare = driver.find_elements(By.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[4]/android.view.View[1]")
if welfare:
print('定位到看广告的任务')
welfare[0].click()
time.sleep(45)
subprocess.getoutput(f"{adb_path} -s {adb_name} shell input tap 489 46")
print('已完成{}次观看广告任务'.format(num))
num += 1
time.sleep(1)
if num == 10:
return
except:
print('出错了,重新开始')
if num == 10:
return
RW(adb_name, num)
adb_name = input('请输入模拟器的编号:')
RW(adb_name)
kuaishou(adb_name)
如果不知道自己的模拟器编号的话,可以按照下面的步骤获取:
- 先打开模拟器
- 再打开win+r 输入命令
adb devices emulator-5558 这个就是你虚拟机的编号名称
使用步骤
为了方便大家,我已经把这些安装包全部放在百度网盘了,大家自行下载吧 链接:https://pan.baidu.com/s/14_D_tLNjcWu3MGI3NyfXbA 提取码:glym
第一个:
安装雷电模拟器,这种的就是傻瓜式安装就好了
第二个:
安装appium,这种的也是傻瓜式安装就好了,然后设置模拟器: 然后在设置里面进入开发者选项
第三个:
把androidsdk这个文件夹放在合适的盘下,再添加到环境变量里面就好了
第四个:
安装jdk环境,然后添加到环境变量里面就好了
第五个:
python安装的第三方库: pip install appium-python-client pip install selenium
第六个
先运行appium 再运行雷电模拟器 最后就可以运行python脚本了
感谢你的阅读,你的三连是我分享的动力!
|