Skip to content

Instantly share code, notes, and snippets.

@dimitryzub
Created June 21, 2021 10:13
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/355c53ed97a409a2696fff985193885d to your computer and use it in GitHub Desktop.
Save dimitryzub/355c53ed97a409a2696fff985193885d to your computer and use it in GitHub Desktop.
youtube_get_video_paylist_results
import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def get_video_paylist_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=dnb playlist')
youtube_playlist = []
for result in driver.find_elements_by_xpath('//*[@id="contents"]/ytd-playlist-renderer'):
playlist_title = result.find_element_by_css_selector('#video-title').text
playlist_link = result.find_element_by_css_selector('.style-scope ytd-playlist-renderer a').get_attribute('href')
channel_name = result.find_element_by_css_selector('#channel-name').text
video_count = result.find_element_by_css_selector('#overlays > ytd-thumbnail-overlay-side-panel-renderer > yt-formatted-string').text
youtube_playlist.append({
'title': playlist_title,
'link': playlist_link,
'count': video_count,
'channel': channel_name,
})
print(json.dumps(youtube_playlist, indent=2, ensure_ascii=False))
get_video_paylist_results()
# part of the output:
'''
[
{
"title": "Drum & Bass Hits Playlist - Top 100 DnB Songs of 2021",
"link": "https://www.youtube.com/watch?v=y3Ko9pP6XAY&list=PLMmqTuUsDkRIZ1C1T2AsVz5XIxtVDfSOe",
"count": "100",
"channel": "Redlist - World Hits"
}
]
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment