下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:4825
该脚本实现以下核心功能:
自动识别抖音窗口并置顶
4
通过模板匹配定位福袋图标和参与按钮
3
5
模拟人类操作节奏防止检测
1
自动处理广告弹窗干扰
2
长时间未发现目标时自动刷新
6
使用前需要准备:
截取福袋图标(fudai_template.png)
截取参与按钮(join_template.png)
截取广告关闭按钮(close_ad.png)
安装Python3.8+和依赖库
import cv2
import numpy as np
import pyautogui
import time
import random
from PIL import ImageGrab
import win32gui
import win32con
全局配置参数
CONFIG = {
'scan_interval': 0.5, # 扫描间隔(秒)
'confidence_threshold': 0.85, # 图像匹配置信度
'max_retry': 3, # 最大重试次数
'debug_mode': True # 调试模式
}
class DouYinFudaiBot:
def init(self):
self.templates = {
'fudai_icon': cv2.imread('fudai_template.png', 0),
'join_button': cv2.imread('join_template.png', 0),
'close_ad': cv2.imread('close_ad.png', 0)
}
self.window_handle = None
self.setup_window()
def setup_window(self):
"""获取抖音窗口句柄"""
def callback(hwnd, extra):
if '抖音' in win32gui.GetWindowText(hwnd):
self.window_handle = hwnd
win32gui.EnumWindows(callback, None)
if self.window_handle:
win32gui.SetForegroundWindow(self.window_handle)
win32gui.ShowWindow(self.window_handle, win32con.SW_MAXIMIZE)
time.sleep(1)
def capture_screen(self):
"""截取当前屏幕"""
if self.window_handle:
rect = win32gui.GetWindowRect(self.window_handle)
return ImageGrab.grab(rect)
return ImageGrab.grab()
def find_template(self, template_key):
"""在屏幕中查找模板图像"""
screen = np.array(self.capture_screen())
gray_screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
template = self.templates[template_key]
res = cv2.matchTemplate(gray_screen, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if max_val > CONFIG['confidence_threshold']:
h, w = template.shape
center_x = max_loc + w // 2
center_y = max_loc^1^ + h // 2
return (center_x, center_y)
return None
def random_delay(self, base=0.5):
"""随机延迟防止检测"""
time.sleep(base + random.uniform(0, 0.3))
def click_position(self, pos):
"""模拟鼠标点击"""
if pos:
pyautogui.moveTo(pos, pos^1^, duration=0.2 + random.random()*0.3)
self.random_delay(0.1)
pyautogui.click()
return True
return False
def handle_ad(self):
"""处理广告弹窗"""
close_pos = self.find_template('close_ad')
if close_pos:
self.click_position(close_pos)
self.random_delay()
return True
return False
def join_fudai(self):
"""参与福袋活动"""
join_pos = self.find_template('join_button')
if join_pos:
self.click_position(join_pos)
self.random_delay(1)
return True
return False
def main_loop(self):
"""主循环逻辑"""
print("抖音福袋机器人启动...")
retry_count = 0
while True:
try:
# 处理广告弹窗
if self.handle_ad():
continue
# 查找福袋图标
fudai_pos = self.find_template('fudai_icon')
if fudai_pos:
print("发现福袋,尝试参与...")
if self.click_position(fudai_pos):
self.random_delay(0.8)
# 尝试点击参与按钮
if self.join_fudai():
print("成功参与福袋!")
retry_count = 0
self.random_delay(3)
continue
# 未找到福袋时的处理
retry_count += 1
if retry_count > CONFIG['max_retry'] * 10:
print("长时间未发现福袋,刷新页面...")
pyautogui.hotkey('f5')
retry_count = 0
self.random_delay(3)
self.random_delay(CONFIG['scan_interval'])
except KeyboardInterrupt:
print("\n程序终止")
break
except Exception as e:
print(f"发生错误: {str(e)}")
self.random_delay(5)
if name == "main":
bot = DouYinFudaiBot()
bot.main_loop()