Last active
April 25, 2017 14:22
-
-
Save quanon/81cd391693a382f73b1a7a7b84c76251 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import time | |
from selenium import webdriver | |
channel = sys.argv[1] | |
driver = webdriver.Chrome() | |
url = os.path.join('https://www.youtube.com/user', channel, 'videos') | |
driver.get(url) | |
scroll_height = 0 | |
while True: | |
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);') | |
time.sleep(1) | |
more = driver.find_elements_by_class_name('load-more-button') | |
if len(more) == 0: | |
break | |
more = more[0] | |
if more.is_displayed(): | |
more.click() | |
curent_scroll_height = driver.execute_script( | |
'return document.body.scrollHeight;') | |
if scroll_height == curent_scroll_height: | |
break | |
scroll_height = curent_scroll_height | |
xpath = '//span[@class="yt-thumb-default"]/span[@class="yt-thumb-clip"]/img' | |
elements = driver.find_elements_by_xpath(xpath) | |
src_list = [element.get_attribute('src') for element in elements] | |
driver.quit() | |
with open(f'{channel}.txt', 'wt') as f: | |
for src in src_list: | |
print(src, file=f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment