下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:7705
这个代码示例展示了如何使用Selenium和PyAutoGUI模拟视频观看行为,包括登录、观看视频、点赞评论等操作。但请注意,实际平台的反爬机制会更复杂,且此类自动化操作违反平台规定。建议仅用于学习自动化测试技术。
import time
import random
import pyautogui
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
class VideoAutoWatcher:
def init(self):
self.driver = None
self.platforms = {
'douyin': 'https://www.douyin.com',
'kuaishou': 'https://www.kuaishou.com',
'xiaohongshu': 'https://www.xiaohongshu.com'
}
self.setup_driver()
def setup_driver(self):
chrome_options = Options()
chrome_options.add_argument("--window-size=1200,800")
chrome_options.add_argument("--mute-audio")
self.driver = webdriver.Chrome(options=chrome_options)
def login(self, platform, username, password):
print(f"Logging in to {platform}...")
self.driver.get(self.platforms[platform])
time.sleep(3)
# 这里简化了登录流程,实际需要根据平台页面结构调整
try:
login_btn = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//button[contains(text(),'登录')]"))
)
login_btn.click()
time.sleep(2)
username_field = self.driver.find_element(By.NAME, "username")
password_field = self.driver.find_element(By.NAME, "password")
username_field.send_keys(username)
password_field.send_keys(password)
password_field.send_keys(Keys.RETURN)
time.sleep(5)
print("Login successful!")
return True
except Exception as e:
print(f"Login failed: {str(e)}")
return False
def watch_videos(self, platform, duration=60, watch_count=10):
print(f"Starting to watch videos on {platform}...")
self.driver.get(self.platforms[platform])
time.sleep(5)
for i in range(watch_count):
print(f"Watching video {i+1}/{watch_count}")
# 模拟观看行为
self.simulate_watching(duration)
# 随机滑动到下一个视频
self.swipe_to_next()
# 随机间隔
time.sleep(random.uniform(2, 5))
def simulate_watching(self, duration):
start_time = time.time()
while time.time() - start_time < duration:
# 随机移动鼠标模拟真实观看
x = random.randint(100, 1000)
y = random.randint(100, 700)
pyautogui.moveTo(x, y, duration=0.5)
# 随机点赞或评论
if random.random() < 0.1:
self.like_or_comment()
time.sleep(random.uniform(1, 3))
def like_or_comment(self):
try:
if random.random() < 0.7:
# 点赞
like_btn = WebDriverWait(self.driver, 5).until(
EC.presence_of_element_located((By.XPATH, "//div[contains(@class,'like-btn')]"))
)
like_btn.click()
print("Liked the video")
else:
# 评论
comment_btn = WebDriverWait(self.driver, 5).until(
EC.presence_of_element_located((By.XPATH, "//div[contains(@class,'comment-btn')]"))
)
comment_btn.click()
time.sleep(1)
comment_box = self.driver.find_element(By.XPATH, "//textarea")
comments = ["不错!", "好看", "666", "赞一个", "喜欢这个内容"]
comment_box.send_keys(random.choice(comments))
time.sleep(1)
submit_btn = self.driver.find_element(By.XPATH, "//button[contains(text(),'发送')]")
submit_btn.click()
print("Commented on the video")
except:
pass
def swipe_to_next(self):
try:
# 模拟滑动操作
pyautogui.moveTo(600, 400, duration=0.5)
pyautogui.dragTo(600, 200, duration=0.5, button='left')
time.sleep(1)
except:
pass
def close(self):
if self.driver:
self.driver.quit()
print("Browser closed.")
if name == "main":
watcher = VideoAutoWatcher()
try:
# 示例使用 - 实际需要替换为真实账号
if watcher.login('douyin', 'your_username', 'your_password'):
watcher.watch_videos('douyin', duration=30, watch_count=20)
finally:
watcher.close()