1

I've been trying to get the upload date of a YouTube video (as the following picture shows) with Selenium in Python.

enter image description here

My code is as follows:

! pip install selenium
from selenium import webdriver
! pip install beautifulsoup4
from bs4 import BeautifulSoup

driver = webdriver.Chrome('D:\chromedrive\chromedriver.exe')
driver.get("https://www.youtube.com/watch?v=CoR72I5BGUU")
soup = BeautifulSoup(driver.page_source, 'lxml')

upload_date=driver.find_elements_by_xpath('//*[@id="info-strings"]/yt-formatted-string')

print(upload_date)

However, no matter I used upload_date=soup.findAll('span', class_='style-scope ytd-grid-video-renderer') or upload_date=driver.find_elements_by_xpath('//*[@id="info-strings"]/yt-formatted-string'), it didn't work.

If anyone knows where the problem is, please help me.

CanaryTheBird
  • 239
  • 1
  • 11

2 Answers2

2

To get the "date", you can use a CSS selector #info-strings yt-formatted-string which will select the HTML ID info-strings and then the info-strings tag.

Also, you should wait for the page to correctly load before trying to scrape the "date" by using WebDriverWait (additional imports are required, see the code example).

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()
driver.get("https://www.youtube.com/watch?v=CoR72I5BGUU")

# Waits 20 seconds for the "date" the appear on the webpage
date = WebDriverWait(driver, 20).until(
    EC.visibility_of_element_located(
        (By.CSS_SELECTOR, "#info-strings yt-formatted-string")
    )
)

print(date.text)

driver.quit()

Output:

Jan 25, 2018

See also:

MendelG
  • 14,885
  • 4
  • 25
  • 52
1

You can try with the below CSS_SELECTOR:

span#dot+yt-formatted-string[class$='ytd-video-primary-info-renderer']

Code:

upload_date = driver.find_element_by_css_selector("span#dot+yt-formatted-string[class$='ytd-video-primary-info-renderer']")
print(upload_date.text)
MendelG
  • 14,885
  • 4
  • 25
  • 52
cruisepandey
  • 28,520
  • 6
  • 20
  • 38