DEV Community

Tony Colston
Tony Colston

Posted on

gotta get down on Friday, scraping view counts from youtube

It is Friday. And you have to get down on Friday.

How many views does RB's video Friday have on Youtube right now?

You can do this with Selenium.

disclaimer this is clearly just for fun, some sites frown on being scraped... :)



ytlink="https://www.youtube.com/watch?v=kfVsfOSbJY0"

from selenium import webdriver
from selenium.webdriver.common.by import By

import time
driver = None

try:
    cpath = "e:\\projects\\headless\\chromedriver.exe"
    driver = webdriver.Chrome(cpath)
    driver.get(ytlink)
    # how do we fix this next statement?
    time.sleep(5)
    cs_selector = "#count > yt-view-count-renderer > span.view-count.style-scope.yt-view-count-renderer"
    e = driver.find_element(By.CSS_SELECTOR,cs_selector)
    time.sleep(3)
    txt_views = print(e.text)


finally:
    if driver is not None:
        driver.quit()
Enter fullscreen mode Exit fullscreen mode

Have a great weekend!!!

Top comments (0)