下载地址:http://m.pan38.com/download.php?code=ENROLN 提取码:3881
代码功能说明:
多平台支持:整合抖音、哔哩哔哩等平台的自动化操作
智能评论生成:结合视频内容动态生成个性化评论
人性化操作:随机延迟和分段输入模拟真实用户行为
稳定运行机制:包含异常处理和循环执行逻辑
配置化管理:通过JSON文件管理账号和目标链接
import random
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from fake_useragent import UserAgent
from datetime import datetime
class SocialMediaAutoBot:
def init(self):
self.ua = UserAgent()
self.config = self._load_config()
self.driver = self._init_driver()
self.action_chain = webdriver.ActionChains(self.driver)
def _load_config(self):
"""加载配置文件"""
with open('config.json', 'r', encoding='utf-8') as f:
return json.load(f)
def _init_driver(self):
"""初始化浏览器驱动"""
chrome_options = Options()
chrome_options.add_argument(f"user-agent={self.ua.random}")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-popup-blocking")
if self.config['headless']:
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options)
driver.set_window_size(1200, 800)
return driver
def _random_delay(self, min_sec=1, max_sec=5):
"""随机延迟模拟人工操作"""
time.sleep(random.uniform(min_sec, max_sec))
def _generate_comment(self, video_content=None):
"""生成智能评论内容"""
if video_content and len(video_content) > 10:
template = random.choice(self.config['ai_templates'])
return template.replace("{keyword}", video_content[:15])
return random.choice(self.config['comments'])
def login_douyin(self):
"""抖音登录流程"""
self.driver.get("https://www.douyin.com")
self._random_delay(3, 5)
try:
login_btn = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//button[contains(text(),'登录')]"))
)
login_btn.click()
self._random_delay(2, 3)
# 切换至账号密码登录
switch_btn = self.driver.find_element(By.XPATH, "//span[contains(text(),'密码登录')]")
switch_btn.click()
self._random_delay()
# 输入账号密码
username = self.driver.find_element(By.NAME, "username")
username.send_keys(self.config['douyin']['username'])
self._random_delay(0.5, 1)
password = self.driver.find_element(By.NAME, "password")
password.send_keys(self.config['douyin']['password'])
self._random_delay(1, 2)
# 提交登录
submit_btn = self.driver.find_element(By.XPATH, "//button[@type='submit']")
submit_btn.click()
WebDriverWait(self.driver, 15).until(
EC.presence_of_element_located((By.XPATH, "//div[contains(@class,'personal-info')]"))
)
print("抖音登录成功")
return True
except Exception as e:
print(f"抖音登录失败: {str(e)}")
return False
def auto_comment_douyin(self, video_url):
"""抖音自动评论"""
try:
self.driver.get(video_url)
self._random_delay(5, 8) # 等待视频加载
# 获取视频描述文本
desc_element = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//div[contains(@class,'desc')]"))
)
video_content = desc_element.text
# 滚动到评论区
comment_section = self.driver.find_element(By.XPATH, "//div[contains(@class,'comment')]")
self.driver.execute_script("arguments[0].scrollIntoView();", comment_section)
self._random_delay(2, 3)
# 点击评论输入框
comment_input = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//div[contains(@class,'comment-input')]//textarea"))
)
comment_input.click()
self._random_delay(1, 2)
# 输入评论内容
comment_text = self._generate_comment(video_content)
for char in comment_text:
comment_input.send_keys(char)
time.sleep(random.uniform(0.05, 0.2)) # 模拟人工输入
self._random_delay(1, 2)
# 发送评论
send_btn = self.driver.find_element(By.XPATH, "//button[contains(@class,'comment-submit')]")
send_btn.click()
print(f"评论成功: {comment_text}")
self._random_delay(3, 5)
return True
except Exception as e:
print(f"评论失败: {str(e)}")
return False
def auto_danmu_bilibili(self, live_url):
"""哔哩哔哩弹幕发送"""
try:
self.driver.get(live_url)
self._random_delay(8, 10) # 等待直播间加载
# 点击弹幕输入框
danmu_input = WebDriverWait(self.driver, 15).until(
EC.presence_of_element_located((By.XPATH, "//div[contains(@class,'chat-input')]//textarea"))
)
danmu_input.click()
self._random_delay(1, 2)
# 输入弹幕内容
danmu_text = random.choice(self.config['danmu'])
for char in danmu_text:
danmu_input.send_keys(char)
time.sleep(random.uniform(0.03, 0.15))
self._random_delay(0.5, 1)
# 发送弹幕
send_btn = self.driver.find_element(By.XPATH, "//button[contains(@class,'send-btn')]")
send_btn.click()
print(f"弹幕发送成功: {danmu_text}")
self._random_delay(4, 6)
return True
except Exception as e:
print(f"弹幕发送失败: {str(e)}")
return False
def run(self):
"""主运行流程"""
if self.login_douyin():
for video_url in self.config['target_videos']:
self.auto_comment_douyin(video_url)
self._random_delay(10, 15) # 间隔时间
for live_url in self.config['bilibili_lives']:
self.auto_danmu_bilibili(live_url)
self._random_delay(20, 30)
if name == "main":
bot = SocialMediaAutoBot()
try:
while True:
bot.run()
print(f"本轮任务完成,等待下次执行... {datetime.now()}")
time.sleep(3600) # 每小时执行一次
except KeyboardInterrupt:
print("程序终止")
finally:
bot.driver.quit()