-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscript_scraper.py
More file actions
63 lines (47 loc) · 2.12 KB
/
Copy pathtranscript_scraper.py
File metadata and controls
63 lines (47 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import sys
import config
# place your own path to webdriver
path = config.FIRE_PATH
def get_title(url):
driver = webdriver.Firefox(executable_path=path)
driver.get(url)
title = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath('//yt-formatted-string[@class="style-scope ytd-video-primary-info-renderer"]'))
ret = title.text
driver.close()
driver.quit()
return ret
def get_transcript(url):
driver = webdriver.Firefox(executable_path=path)
driver.get(url)
moreActionsButton = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath('//button[@id="button"][@aria-label="More actions"]'))
# select more actions button
moreActionsButton.click()
openTrans = WebDriverWait(driver,10).until(lambda driver: driver.find_element_by_xpath('//paper-item[@class="style-scope ytd-menu-service-item-renderer"][@role="option"]'))
# open transcript window
openTrans.click()
# make sure transcript window is open
testTrans = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath('//div[@class="cue style-scope ytd-transcript-body-renderer"][@role="button"]'))
transcript = driver.find_elements_by_xpath('//div[@class="cue style-scope ytd-transcript-body-renderer"][@role="button"]')
title = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath('//yt-formatted-string[@class="style-scope ytd-video-primary-info-renderer"]')).text
# write out transcript to file named after the video title
f = open(title+".txt", "w+")
for element in transcript:
f.write(element.text)
f.write('\n')
f.close()
# close the driver
driver.close()
driver.quit()
return title
if len(sys.argv) < 2:
print('Error - Not enough arguments.')
else:
url = sys.argv[1]
if 'youtube' in url:
get_transcript(url)
else:
print('Error - Enter a Youtube video URL.')