Skip to content

Instantly share code, notes, and snippets.

@dimitryzub
Created June 21, 2021 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitryzub/da76737c9e69f4336221922aba214091 to your computer and use it in GitHub Desktop.
Save dimitryzub/da76737c9e69f4336221922aba214091 to your computer and use it in GitHub Desktop.
youtube_get_category_results
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def get_category_results():
options = Options()
# running selenium in headless mode
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get('https://www.youtube.com/results?search_query=mojang')
for result in driver.find_elements_by_css_selector('#contents > ytd-vertical-list-renderer'):
title = result.find_element_by_css_selector('#video-title > yt-formatted-string').text
link = result.find_element_by_css_selector('#video-title').get_attribute('href')
views = result.find_element_by_css_selector('#metadata-line > span:nth-child(1)').text
date_posted = result.find_element_by_css_selector('#metadata-line > span:nth-child(2)').text
snippet = result.find_element_by_css_selector('#dismissible > div > div.metadata-snippet-container.style-scope.ytd-video-renderer > yt-formatted-string').text
channel_name = result.find_element_by_css_selector('.long-byline').text
channel_link = result.find_element_by_css_selector('#text > a').get_attribute('href')
try:
badges = result.find_element_by_css_selector('#badges').text
except:
badges = None
print(f'{title}\n{link}\n{views}\n{date_posted}\n{snippet}\n{channel_name}\n{channel_link}\n{badges}\n')
get_category_results()
# part of the output:
'''
Ask Mojang #18: More Mobs!
https://www.youtube.com/watch?v=miKrxo3uaXU
574K views
2 days ago
Matthew, Anna, and Thommy answer your questions about the glorious dirt block. Just kidding, they're here to talk about mobs!
Minecraft
https://www.youtube.com/user/TeamMojang
New
CC
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment