Skip to content

抖音自动刷视频

环境搭建

python

appium

木木模拟器

下载 抖音 apk 包并安装到模拟器中

相关命令

  • python 安装 Appium-Python-Client
sh
pip install Appium-Python-Client
  • 启动 appium 服务
  • 连接模拟器
sh
# 木木模拟器
adb connect 127.0.0.1:7555
# 夜神模拟器
adb connect 127.0.0.1:62001
  • 查看设备
sh
adb devices
  • 查看当前运行的 APP 包名 appPackage 和活动页 appActivity

    注意要先在模拟器打开 抖音极速版APP,否则看不到相关信息

sh
adb shell dumpsys window | findstr mCurrentFocus

# mCurrentFocus=Window{a179906 u0 com.ss.android.ugc.aweme/com.ss.android.ugc.aweme.splash.SplashActivity}

包名: com.ss.android.ugc.aweme 活动页: com.ss.android.ugc.aweme.splash.SplashActivity

python 脚本

py
import time

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

desired_caps = {
    "platformName": "Android",
    "deviceName": "127.0.0.1:7555",  # 通过 adb devices命令获取
    "appPackage": "com.ss.android.ugc.aweme",
    "appActivity": "com.ss.android.ugc.aweme.splash.SplashActivity",
    "appWaitDuration": 200000
}

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)

driver.implicitly_wait(20)  # 等待弹框元素
driver.find_element(By.XPATH, "//*[@text='同意']").click()
driver.implicitly_wait(20)  # 等待弹框元素
driver.find_element(By.XPATH, "//*[@text='允许']").click()

time.sleep(1.5)
size = driver.get_window_size()
width, height = size['width'], size['height']  # 获取屏幕宽高

action = TouchAction(driver)
for i in range(5):
    # 实现滑动
    action.press(x=width / 2, y=height / 3 * 2).wait(500).move_to(x=width / 2, y=height / 3).release()
    action.perform()
    try:
        # 3s后 弹框元素不出现,直接跳过继续执行
        WebDriverWait(driver, 3).until(ec.presence_of_element_located((By.XPATH, "//*[@text='允许']")))
        driver.find_element(By.XPATH, "//*[@text='允许']").click()
    except:
        pass

time.sleep(5)
driver.close_app()
driver.quit()

参考

Python自动化抖音自动刷视频