如何获取YouTube视频';使用Selenium和Python的持续时间/长度?
我正试图提取一个YT频道所有视频的标题、持续时间和链接。我使用selenium和python的方式如下:如何获取YouTube视频';使用Selenium和Python的持续时间/长度?,python,selenium,Python,Selenium,我正试图提取一个YT频道所有视频的标题、持续时间和链接。我使用selenium和python的方式如下: import time from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() results = [] url = "https://www.youtube.com/channel/<channel name&g
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
results = []
url = "https://www.youtube.com/channel/<channel name>/videos"
driver.get(url)
ht=driver.execute_script("return document.documentElement.scrollHeight;")
while True:
prev_ht=driver.execute_script("return document.documentElement.scrollHeight;")
driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
time.sleep(2)
ht=driver.execute_script("return document.documentElement.scrollHeight;")
if prev_ht==ht:
break
links=driver.find_elements_by_xpath('//*[@class="style-scope ytd-grid-renderer"]')
for link in links:
print()
print(link.get_attribute("href"), link.get_attribute("text"))
导入时间
从selenium导入webdriver
从selenium.webdriver.common.keys导入密钥
driver=webdriver.Chrome()
结果=[]
url=”https://www.youtube.com/channel//videos"
获取驱动程序(url)
ht=driver.execute_脚本(“returndocument.documentElement.scrollHeight;”)
尽管如此:
prev\u ht=driver.execute\u脚本(“return document.documentElement.scrollHeight;”)
执行脚本(“window.scrollTo(0,document.documentElement.scrollHeight);”)
时间。睡眠(2)
ht=driver.execute_脚本(“returndocument.documentElement.scrollHeight;”)
如果上一个ht==ht:
打破
links=driver。通过xpath查找元素('/*[@class=“style scope ytd grid renderer”]”)
对于链接中的链接:
打印()
打印(link.get_属性(“href”)、link.get_属性(“文本”))
当我尝试使用class=“style scope ytd缩略图覆盖时间状态渲染器”
class获取视频的持续时间时,驱动程序返回该元素不存在。不过,我还是完成了其他两项功能。早上好
如果光标不在最佳位置,Selenium可能无法获取视频持续时间。这里有一个GIF显示:。你可以通过使用Youtube的一些内置Javascript函数来解决这个问题。下面是一个使用此方法的示例:
video_dur = self.driver.execute_script(
"return document.getElementById('movie_player').getCurrentTime()")
video_len = self.driver.execute_script(
"return document.getElementById('movie_player').getDuration()")
video_len = int(video_len) / 60
祝你今天愉快 您的XPath定位器不正确,因此请使用以下内容:
links=driver.find_elements_by_xpath('//*[name() = "ytd-grid-video-renderer" and @class="style-scope ytd-grid-renderer"]')
links=driver.find_elements_by_xpath('//*[name() = "ytd-grid-video-renderer" and @class="style-scope ytd-grid-renderer"]')
for link in links:
duration = link.find_element_by_xpath('.//span[contains(@class,"time-status")]').text
print(duration)
现在,要获得定义的每个链接的视频长度,可以执行以下操作:
links=driver.find_elements_by_xpath('//*[name() = "ytd-grid-video-renderer" and @class="style-scope ytd-grid-renderer"]')
links=driver.find_elements_by_xpath('//*[name() = "ytd-grid-video-renderer" and @class="style-scope ytd-grid-renderer"]')
for link in links:
duration = link.find_element_by_xpath('.//span[contains(@class,"time-status")]').text
print(duration)
但是,selenium.common.exceptions.NoSuchElementException不起作用:消息:没有这样的元素:找不到元素:{“方法”:“xpath”,“选择器”:“//span[contains(@class,'time-status')]”(Session info:chrome=90.0.4430.212)您的web元素列表是否包含web元素列表?我看到我这边有60个元素。是的,它有一个视频列表。我又看到了,发现你的定位器不正确。请查看更新的答案,并让我知道它现在是否工作正确是的,我没有使用硒那么多,但您上次的更新使其工作良好。