下载地址:https://www.pan38.com/yun/share.php?code=JCnzE 提取密码:9987
这个脚本实现了快手批量上传视频的功能,包含登录、上传视频、添加描述和发布等完整流程。使用时需要安装Chrome浏览器和对应版本的chromedriver,并将视频文件放在指定文件夹中。
import os
import time
import logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
class KuaiShouUploader:
def init(self, username, password, video_folder):
self.username = username
self.password = password
self.video_folder = video_folder
self.setup_logging()
self.driver = self.setup_driver()
def setup_logging(self):
logging.basicConfig(
filename='kuai_shou_uploader.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
self.logger = logging.getLogger(__name__)
def setup_driver(self):
options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
options.add_argument('--disable-infobars')
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--window-size=1920,1080')
service = Service(executable_path='chromedriver')
driver = webdriver.Chrome(service=service, options=options)
driver.implicitly_wait(10)
return driver
def login(self):
self.logger.info("开始登录快手账号")
self.driver.get('https://www.kuaishou.com')
try:
login_button = WebDriverWait(self.driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '//button[contains(text(),"登录")]'))
)
login_button.click()
# 切换到账号密码登录
switch_method = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//div[contains(text(),"账号密码登录")]'))
)
switch_method.click()
# 输入用户名和密码
username_input = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//input[@placeholder="请输入手机号/邮箱"]'))
)
username_input.send_keys(self.username)
password_input = self.driver.find_element(By.XPATH, '//input[@placeholder="请输入密码"]')
password_input.send_keys(self.password)
# 点击登录按钮
submit_button = self.driver.find_element(By.XPATH, '//button[contains(text(),"登录")]')
submit_button.click()
# 等待登录成功
WebDriverWait(self.driver, 30).until(
EC.presence_of_element_located((By.XPATH, '//div[contains(@class,"user-info")]'))
)
self.logger.info("登录成功")
return True
except Exception as e:
self.logger.error(f"登录失败: {str(e)}")
return False
def upload_video(self, video_path, description):
self.logger.info(f"开始上传视频: {video_path}")
try:
# 打开发布页面
self.driver.get('https://www.kuaishou.com/publish')
# 上传视频文件
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))
# 等待视频上传完成
WebDriverWait(self.driver, 300).until(
EC.invisibility_of_element_located((By.XPATH, '//div[contains(text(),"上传中")]'))
)
# 输入视频描述
desc_input = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//textarea[@placeholder="添加描述..."]'))
)
desc_input.send_keys(description)
# 点击发布按钮
publish_button = WebDriverWait(self.driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '//button[contains(text(),"发布")]'))
)
publish_button.click()
# 等待发布完成
WebDriverWait(self.driver, 60).until(
EC.presence_of_element_located((By.XPATH, '//div[contains(text(),"发布成功")]'))
)
self.logger.info(f"视频发布成功: {video_path}")
return True
except Exception as e:
self.logger.error(f"视频上传失败 {video_path}: {str(e)}")
return False
def batch_upload(self):
if not os.path.exists(self.video_folder):
self.logger.error(f"视频文件夹不存在: {self.video_folder}")
return False
video_files = [f for f in os.listdir(self.video_folder) if f.endswith(('.mp4', '.mov', '.avi'))]
if not video_files:
self.logger.error("文件夹中没有找到视频文件")
return False
if not self.login():
return False
for idx, video_file in enumerate(video_files, 1):
video_path = os.path.join(self.video_folder, video_file)
description = f"这是我的第{idx}个作品 #{video_file.split('.')[0]}"
if self.upload_video(video_path, description):
self.logger.info(f"成功上传第{idx}个视频: {video_file}")
else:
self.logger.warning(f"上传第{idx}个视频失败: {video_file}")
# 间隔一段时间再上传下一个
time.sleep(10)
return True
def close(self):
self.driver.quit()
self.logger.info("浏览器已关闭")
if name == "main":
# 配置参数
USERNAME = "your_username"
PASSWORD = "your_password"
VIDEO_FOLDER = "videos"
uploader = KuaiShouUploader(USERNAME, PASSWORD, VIDEO_FOLDER)
try:
uploader.batch_upload()
except Exception as e:
uploader.logger.error(f"程序运行出错: {str(e)}")
finally:
uploader.close()