import uiautomator2 as u2
import time
import random
# ----------------------- 配置区(可根据需求修改) -----------------------
DOUYIN_PKG = "com.ss.android.ugc.aweme"
LIKE_PROB = 0.4 # 40%的概率给视频点赞
COMMENT_PROB = 0.1 # 10%的概率发评论
MAX_VIDEOS = 15 # 最多刷15个视频就停止
COMMENT_LIST = [
"很棒的内容~",
"学到了,收藏收藏!",
"支持一下博主!",
"这个好有意思哈哈哈哈",
"666666"
]
# ----------------------- 连接设备 -----------------------
print("🔗 正在连接安卓设备...")
try:
d = u2.connect() # 也可以写u2.connect("192.168.1.100")连WiFi
print(f"✅ 连接成功!设备序列号:{d.serial}")
except Exception as e:
print(f"❌ 连接失败,请检查USB调试/网络连接:{e}")
exit()
# ----------------------- 安全操作工具函数 -----------------------
def safe_click(element, timeout=2):
"""带超时和随机偏移的安全点击"""
if not element.exists(timeout=timeout):
return False
bounds = element.bounds
# 避免点击元素边缘(边缘可能是无效区域
x = random.randint(bounds['left'] + 6, bounds['right'] - 6)
y = random.randint(bounds['top'] + 6, bounds['bottom'] - 6)
d.click(x, y)
return True
def random_delay(base, variance):
"""随机延时(base为基准秒数,variance为波动范围)"""
actual_delay = max(0.5, base + random.uniform(-variance, variance))
time.sleep(actual_delay)
# ----------------------- 主运行逻辑 -----------------------
total_videos = 0
total_likes = 0
total_comments = 0
print(f"\n🚀 脚本启动!目标:最多刷{MAX_VIDEOS}个视频")
try:
while total_videos < MAX_VIDEOS:
total_videos += 1
print(f"\n🎬 正在处理第{total_videos}/{MAX_VIDEOS}个视频...")
# 先「看一会儿」视频,模拟真人停留
random_delay(2.5, 1)
# 1. 随机点赞
if random.random() < LIKE_PROB:
# 尝试多种定位方式(避免一种方式失效)
like_element = (
d(resourceId="com.ss.android.ugc.aweme:id/aweme_like_layout")
or d(desc="赞")
or d(resourceId="com.ss.android.ugc.aweme:id/dv9") # 备用ID(部分抖音版本可能不同
)
if safe_click(like_element):
total_likes += 1
print(f"👍 点赞成功!累计点赞:{total_likes}")
random_delay(0.8, 0.3)
# 2. 随机评论
if random.random() < COMMENT_PROB:
# 点击评论按钮
comment_btn = (
d(resourceId="com.ss.android.ugc.aweme:id/aweme_comment_layout")
or d(desc="评论")
or d(resourceId="com.ss.android.ugc.aweme:id/dvb")
)
if safe_click(comment_btn):
random_delay(1.5, 0.5)
# 找评论输入框
input_box = (
d(resourceId="com.ss.android.ugc.aweme:id/chat_input_view")
or d(className="android.widget.EditText")
)
if input_box.exists(timeout=2):
# 输入随机评论
random_comment = random.choice(COMMENT_LIST)
input_box.set_text(random_comment)
random_delay(0.9, 0.2)
# 找发送按钮
send_btn = d(text="发送") or d(desc="发送")
if safe_click(send_btn):
total_comments += 1
print(f"💬 评论成功!内容:{random_comment} | 累计评论:{total_comments}")
random_delay(1, 0.3)
# 返回视频播放页
d.press("back")
random_delay(0.8, 0.3)
# 3. 上滑到下一个视频
print("⬆️ 滑动到下一个视频...")
d.swipe_ext(
"up",
scale=0.7 + random.random()*0.1,
duration=0.8 + random.random()*0.2
)
# 等待下一个视频加载
random_delay(1.2, 0.5)
except KeyboardInterrupt:
print("\n⏸️ 用户手动停止了脚本")
except Exception as e:
print(f"\n❌ 脚本运行出错:{e}")
finally:
print("\n📊 本次运行统计:")
print(f" 处理视频数:{total_videos}")
print(f" 成功点赞数:{total_likes}")
print(f" 成功评论数:{total_comments}")