所以,我想用selenium来获取youtube视频的持续时间,我正在尝试使用这个:
duration = self.browser.find_elements_by_xpath("//span[@class='ytp-time-duration']")[0]
pauseButton.click()
print('Video Length = '+duration.text)
但这大多只是一个错误,因为视频持续时间并不总是显示的。只有当鼠标悬停在视频播放器上或视频暂停时,它才会显示。有人知道我如何使用python Selenium获取任何youtube视频的持续时间吗?
试试这段代码,它会找到持续时间,并以秒为单位获得持续时间。
from selenium import webdriver
import time, datetime
# create the driver and take in the profile preferences
driver = webdriver.Firefox()
# navigate to the below youtube video
driver.get(url)
# Obtain the length of the youtube video
duration = driver.find_elements_by_xpath("//span[@class='ytp-time-duration']")[0].text
# Obtain the length of the video in seconds
x = time.strptime(duration, '%M:%S')
x1 = datetime.timedelta(minutes=x.tm_min, seconds=x.tm_sec).total_seconds()
print(x1)