代码实现
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"
# Appium Server连接的参数
desired_caps = {
'platformName': 'Android', # 被测手机是安卓
'platformVersion': '7', # 手机安卓版本
'deviceName': 'xxx', # 设备名,安卓手机可以随意填写
'appPackage': 'com.kuaishou.nebula', # 启动APP Package名称
'appActivity': 'com.yxcorp.gifshow.HomeActivity', # 启动Activity名称
'unicodeKeyboard': True, # 使用自带输入法,输入中文时填True
'resetKeyboard': True, # 执行完程序恢复原来输入法
'noReset': True, # 不要重置App
'newCommandTimeout': 6000,
'automationName' : 'UiAutomator2'
}
# 连接Appium Server,初始化自动化环境
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 |