Python+selenium 自動化 - 快手短視頻發布
- 第一章:效果展示
- ① 效果展示
- ② 素材展示
- 第二章:實作程序
- ① 呼叫已啟用的瀏覽器
- ② 上傳視頻和圖片
- ③ 完整原始碼展示
第一章:效果展示
① 效果展示
很多人學習python,不知道從何學起, 很多人學習python,掌握了基本語法過后,不知道在哪里尋找案例上手, 很多已經做案例的人,卻不知道如何去學習更加高深的知識, 那么針對這三類人,我給大家提供一個好的學習平臺,免費領取視頻教程,電子書籍,以及課程的源代碼! QQ群:701698587 歡迎加入,一起討論 一起學習!
② 素材展示
一個為視頻,另一個為像素大小不小于視頻的封面,
第二章:實作程序
① 呼叫已啟用的瀏覽器
通過呼叫已啟用的瀏覽器,可以實作直接跳過每次的登錄程序,
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/309008.html
標籤:其他
上一篇:JAVA 考試系統模塊設計方案
