【问题标题】:How to get the duration of a youtube video using selenium [python]如何使用 selenium [python] 获取 youtube 视频的持续时间
【发布时间】:2020-05-03 13:09:17
【问题描述】:

所以,我想使用 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 视频的持续时间?

【问题讨论】:

  • 你厌倦了 YouTube Api 吗?
  • 视频何时播放或是否在搜索屏幕上?
  • @AomineDaici 视频播放时

标签: python python-3.x selenium selenium-chromedriver


【解决方案1】:

试试这段代码,它会找到持续时间并以秒为单位获得持续时间。

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)

【讨论】:

  • 如果视频前有广告,则返回广告时长。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 2020-06-05
  • 2015-11-24
  • 2017-07-14
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多