Skip to main content
 首页 » 编程设计

python之为 Youtube 自动生成的字幕自动打开脚本

2024年12月31日10fff_TT

我正在尝试编写一个 python 脚本,它将自动点击打开 YouTube 视频的脚本。

例如,如果您转到一个 youtube 视频,在共享按钮右侧的两个位置(由 3 个点表示),您可以打开 youtube 视频的文字记录。

我目前使用的代码打开 URL,并打开隐藏式字幕(有效)。

from selenium import webdriver 
import requests 
 
driver = webdriver.Chrome() 
driver.get("https://www.youtube.com/watch?v=q1RYI034sH0") 
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "ytp-subtitles-button"))) 
element.click() 

但是,我知道要通过添加以下代码来打开成绩单,这会产生错误,因为它找不到按钮。

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "yt-icon-button)"))) 

我不太确定从这里开始做什么,非常感谢任何帮助自动打开 youtube 的成绩单。

请您参考如下方法:

试试这段代码:

# opens 'More actions' menu 
more_action_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label = 'More actions']"))) 
more_action_btn.click() 
 
# clicks on 'open transcription' button     
open_trancript_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//paper-listbox[@id = 'items']/ytd-menu-service-item-renderer"))) 
open_trancript_btn.click()