-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.py
31 lines (26 loc) · 942 Bytes
/
youtube.py
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
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
browser = webdriver.Chrome()
browser.get("https://www.youtube.com/")
search_bar = browser.find_element_by_id('search')
search_bar.send_keys('NAL i5k')
search_bar.send_keys(Keys.ENTER)
#search_icon = browser.find_element_by_id('search-icon-legacy')
#search_icon.click()
browser.get(browser.current_url)
all_videos = browser.find_elements_by_id('video-title')
#all_videos = browser.find_elements_by_xpath('//*[@id="video-title"]')
#all_videos = browser.find_elements_by_tag_name('a')
for video in all_videos:
video_url = video.get_attribute('href')
video_ID = video_url.split('?v=')[-1]
print(video_ID)
print("Finish youtube scraping")
'''
##right click and click "copy link address"
#actionChains = ActionChains(browser)
#actionChains.context_click(first_video).perform()
#time.sleep(3)
'''