成品下载地址:https://www.pan38.com/yun/share.php?code=JCnzE 提取密码:1153
该实现包含完整的登录验证、视频上传和批量处理功能,通过Selenium模拟浏览器操作。使用时需要安装ChromeDriver并配置对应版本的Chrome浏览器,主要还是用易语言开发的,分享的是python代码哈,借助队长浏览器模块实现。
源码部分:【仅供学习测试】
import os
import time
import random
import requests
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 DouyinUploader:
def __init__(self, username, password):
self.username = username
self.password = password
self.driver = None
self.session = requests.Session()
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
def init_driver(self):
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--mute-audio")
self.driver = webdriver.Chrome(options=chrome_options)
self.driver.maximize_window()
def login(self):
self.driver.get("https://www.douyin.com")
time.sleep(3)
# 点击登录按钮
login_btn = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="douyin-header"]/div/div[3]/div[2]/div'))
)
login_btn.click()
time.sleep(2)
# 切换到账号密码登录
switch_btn = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="login-pannel"]/div[2]/div[1]/div[1]/div[2]'))
)
switch_btn.click()
time.sleep(1)
# 输入账号密码
username_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="login-pannel"]/div[2]/div[1]/div[2]/div[1]/input'))
)
username_input.send_keys(self.username)
password_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="login-pannel"]/div[2]/div[1]/div[2]/div[2]/input'))
)
password_input.send_keys(self.password)
time.sleep(1)
# 点击登录
submit_btn = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="login-pannel"]/div[2]/div[1]/div[2]/button'))
)
submit_btn.click()
time.sleep(5)
# 验证登录成功
try:
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="douyin-header"]/div/div[3]/div[3]/div'))
)
print("登录成功")
return True
except:
print("登录失败")
return False
def upload_video(self, video_path, description="", tags=[]):
# 进入发布页面
self.driver.get("https://creator.douyin.com/content/upload")
time.sleep(5)
# 上传视频文件
upload_input = WebDriverWait(self.driver, 30).until(
EC.presence_of_element_located((By.XPATH, '//input[@type="file"]'))
)
upload_input.send_keys(os.path.abspath(video_path))
print(f"开始上传视频: {video_path}")
# 等待上传完成
time.sleep(10)
try:
WebDriverWait(self.driver, 120).until(
EC.presence_of_element_located((By.XPATH, '//*[contains(text(),"上传完成")]'))
)
print("视频上传完成")
except:
print("视频上传超时")
return False
# 输入描述
if description:
desc_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//textarea[@placeholder="添加描述..."]'))
)
desc_input.send_keys(description)
time.sleep(1)
# 添加标签
if tags:
for tag in tags:
tag_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//input[@placeholder="添加话题"]'))
)
tag_input.send_keys(f"#{tag}")
time.sleep(1)
tag_input.send_keys(Keys.ENTER)
time.sleep(1)
# 点击发布
publish_btn = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[contains(@class,"publish-btn")]'))
)
publish_btn.click()
print("视频发布中...")
time.sleep(10)
# 验证发布成功
try:
WebDriverWait(self.driver, 30).until(
EC.presence_of_element_located((By.XPATH, '//*[contains(text(),"发布成功")]'))
)
print("视频发布成功")
return True
except:
print("视频发布失败")
return False
def batch_upload(self, video_dir, descriptions=[], tags=[]):
if not os.path.exists(video_dir):
print(f"目录不存在: {video_dir}")
return
video_files = [f for f in os.listdir(video_dir) if f.endswith(('.mp4', '.mov', '.avi'))]
if not video_files:
print("目录中没有视频文件")
return
for i, video_file in enumerate(video_files):
video_path = os.path.join(video_dir, video_file)
desc = descriptions[i] if i < len(descriptions) else ""
print(f"\n开始处理第 {i+1}/{len(video_files)} 个视频: {video_file}")
retry = 3
while retry > 0:
if self.upload_video(video_path, desc, tags):
break
retry -= 1
print(f"上传失败,剩余重试次数: {retry}")
time.sleep(10)
# 随机间隔防止频繁操作
sleep_time = random.randint(30, 120)
print(f"等待 {sleep_time} 秒后继续...")
time.sleep(sleep_time)
def close(self):
if self.driver:
self.driver.quit()
print("程序退出")
if __name__ == "__main__":
# 使用示例
uploader = DouyinUploader("your_username", "your_password")
try:
uploader.init_driver()
if uploader.login():
# 批量上传视频
video_dir = "./videos"
descriptions = [
"这是第一个测试视频 #测试",
"第二个视频内容 #抖音助手",
"自动化上传演示 #python"
]
tags = ["测试", "自动化", "python"]
uploader.batch_upload(video_dir, descriptions, tags)
finally:
uploader.close()
[account]
username = your_douyin_account
password = your_password
[upload]
video_dir = ./videos
min_interval = 30
max_interval = 120
default_tags = 测试,自动化,python
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。