华域联盟 Python Python+selenium 自动化快手短视频发布的实现过程

Python+selenium 自动化快手短视频发布的实现过程

第一章:效果展示

① 效果展示

② 素材展示

一个为视频,另一个为像素大小不小于视频的封面。

第二章:实现过程

① 调用已启用的浏览器

通过调用已启用的浏览器,可以实现直接跳过每次的登录过程。

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:5003")
driver = webdriver.Chrome(options = options)

② 上传视频和图片

上传功能的使用方法可以查看:

# 上传本地视频
driver.find_element_by_xpath('//input[@type="file"]').send_keys(path_mp4)

# 添加封面
time.sleep(2)
driver.find_element_by_xpath('//button//*[contains(text(),"编辑封面")]').click()
# 进入iframe框架
driver.switch_to.frame(driver.find_element_by_xpath('//iframe'))
time.sleep(1)
driver.find_element_by_xpath('//input[@type="file"]').send_keys(path_cover)
time.sleep(10)
driver.find_element_by_xpath('//button[text()="确定"]').click()
# 退出默认框架
driver.switch_to_default_content()
③ 完整源码展示
import selenium
from selenium import webdriver
import pathlib
import time
from selenium.webdriver.common.keys import Keys

# 基本信息
# 视频存放路径
catalog_mp4 = r"C:\Users\Administrator\Desktop\视频发布"
# 视频描述
describe = "裸眼3D看蜘蛛侠 #搞笑 #电影 #视觉震撼"
time.sleep(10)
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:5003")
driver = webdriver.Chrome(options = options)

path = pathlib.Path(catalog_mp4)

# 视频地址获取
path_mp4 = ""
for i in path.iterdir():
    if(".mp4" in str(i)):
        path_mp4 = str(i);
        break;

if(path_mp4 != ""):
    print("检查到视频路径:" + path_mp4)
else:
    print("未检查到视频路径,程序终止!")
    exit()

# 封面地址获取
path_cover = ""
for i in path.iterdir():
    if(".png" in str(i) or ".jpg" in str(i)):
        path_cover = str(i);
        break;

if(path_cover != ""):
    print("检查到封面路径:" + path_cover)
else:
    print("未检查到封面路径,程序终止!")
    exit()
    
def publish_kuaishou():
    '''
     作用:发布快手视频
    '''
    
    # 进入创作者页面,并上传视频
    driver.get("https://cp.kuaishou.com/article/publish/video?origin=www.kuaishou.com")
    time.sleep(3)
    driver.find_element_by_xpath('//input[@type="file"]').send_keys(path_mp4)
    
    # 等待视频上传完成
    while True:
        time.sleep(3)
        try:
            driver.find_element_by_xpath('//*[contains(text(),"上传成功")]')
            break;
        except Exception as e:
            print("视频还在上传中···")
    
    print("视频已上传完成!")
    
    # 添加封面
    time.sleep(2)
    driver.find_element_by_xpath('//button//*[contains(text(),"编辑封面")]').click()
    # 进入iframe框架
    driver.switch_to.frame(driver.find_element_by_xpath('//iframe'))
    time.sleep(1)
    driver.find_element_by_xpath('//input[@type="file"]').send_keys(path_cover)
    time.sleep(10)
    driver.find_element_by_xpath('//button[text()="确定"]').click()
    # 退出默认框架
    driver.switch_to_default_content()
    
    # 切换常规视频
    time.sleep(2)
    driver.find_element_by_xpath('//*[contains(text(),"去上传常规视频")]').click()
    
    time.sleep(3)
    # 输入视频描述
    driver.find_element_by_xpath('//*[@placeholder="添加合适的话题和描述,作品能获得更多推荐~"]').send_keys(describe)
    
    # 选择分类
    driver.find_element_by_xpath('//*[@placeholder="请选择"]').click()
    time.sleep(2)
    driver.find_element_by_xpath('//*[text()="影视"]').click()
    time.sleep(1)
    
    # 人工进行检查并发布
    # time.sleep(3)
    # # 点击发布
    # driver.find_element_by_xpath('//*[text()="发布"]').click()

# 开始执行视频发布
publish_kuaishou()

到此这篇关于Python+selenium 自动化快手短视频发布的文章就介绍到这了,更多相关Python selenium 自动化 内容请搜索华域联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持华域联盟!

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » Python+selenium 自动化快手短视频发布的实现过程

转载请保留出处和原文链接:https://www.cnhackhy.com/38332.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部