Scraping subtitles of a YouTube's video with selenium.webdriver

时间:2018-09-22 22:54:08

标签: python html selenium

I'm trying to scrape youtube's video subtitles (captions), but I can't to choose a certan language. For example, when I try to scrape subtitles of this video: "https://www.youtube.com/watch?v=GrsJDy8VjZk", I get the link to the first subtitles (in this case it is English (Great Britain) subtitles).

Here is my code:

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

def subtitles_link(driver):
time.sleep(1)
tmg = driver.execute_script("return window.performance.getEntries();")

# Find string in timings that contains the substring 'srv3'
link = ""
for line in tmg:
     for val in line.values():
         if "srv3" in str(val):
             link = val
return link

driver = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')
driver.get('https://www.youtube.com/watch?v=GrsJDy8VjZk')
elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "ytp-subtitles-button")))
elem.click()
link = subtitles_link(driver)

Is there a way to choose a certain subtitles or scrape all of certain youtube's video subtitles?

0 个答案:

没有答案