天天看點

Python+selenium 實作自動上傳并釋出好看短視訊執行個體示範

第一章:效果展示

① 效果展示

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('//*[text()="釋出視訊"]').click()
time.sleep(2)
driver.find_element_by_xpath('//input[@type="file"]').send_keys(path_mp4)

# 添加封面
driver.find_element_by_xpath('//*[text()="上傳封面"]').click()
time.sleep(5)
driver.find_element_by_xpath('//*[text()="本地上傳"]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@class="image-uploader-container"]//input[@type="file"]').send_keys(path_cover)
time.sleep(5)
driver.find_element_by_xpath('//*[text()="完成"]').click()
      

③ 完整源碼展示

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_haokan():
    '''
     作用:釋出好看視訊
    '''

    # 進入創作者頁面,并上傳視訊
    driver.get("https://dream.haokan.com/author/upload")
    time.sleep(2)
    driver.find_element_by_xpath('//input[@type="file"]').send_keys(path_mp4)

    # 等待視訊上傳完成
    while True:
        time.sleep(3)
        try:
            driver.find_element_by_xpath('//*[text()="上傳成功"]')
            break;
        except Exception as e:
            print("視訊還在上傳中···")

    print("視訊已上傳完成!")

    # 選擇分類
    driver.find_element_by_xpath('//*[@class="hk-select-selection-item"]').click()
    time.sleep(2)
    driver.find_element_by_xpath('//*[@title="影視"]').click()
    time.sleep(1)

    # 添加封面
    driver.find_element_by_xpath('//*[text()="上傳封面"]').click()
    time.sleep(5)
    driver.find_element_by_xpath('//*[text()="本地上傳"]').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@class="image-uploader-container"]//input[@type="file"]').send_keys(path_cover)
    time.sleep(5)
    driver.find_element_by_xpath('//*[text()="完成"]').click()

    time.sleep(2)
    # 輸入标題
    driver.find_element_by_xpath('//*[@class="input-content"]//input').send_keys(Keys.CONTROL, 'a')
    time.sleep(2)
    driver.find_element_by_xpath('//*[@class="input-content"]//input').send_keys(describe)

    # 輸入視訊描述
    driver.find_element_by_xpath('//textarea').send_keys(describe)

    # 位置
    time.sleep(1)
    driver.find_element_by_xpath('//*[@class="location-input-wrap"]//input').send_keys("中關村人工智能科技園")
    time.sleep(3)
    driver.find_element_by_xpath('//*[text()="中關村人工智能科技園"]').click()

    # 參加話題
    time.sleep(1)
    driver.find_element_by_xpath('//*[text()="展開"]').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[text()="好看創作季"]').click()
    time.sleep(1)

    # 人工進行檢查并釋出
    # time.sleep(3)
    # # 點選釋出
    # driver.find_element_by_xpath('//*[text()="釋出"]').click()

# 開始執行視訊釋出
publish_haokan()