Youtube + Selenium ChromeDriver(Python)-如何知道视频何时结束? [英] Youtube + Selenium ChromeDriver (Python) - How to know when a video ends?
问题描述
关于了解其中正在播放视频的Youtube页面内容的简短问题:
总结
作为基于浏览器的GUI的一部分,我使用Selenium在Youtube上播放视频.
我需要知道视频何时结束播放,以便GUI进行其他操作(例如,将本地HTML馈入浏览器).
代码段和问题
import os, time
from selenium import webdriver
# [...]
chromedriver = 'path_to_chromedriver_binary' # substitute as appropriate
driver = webdriver.Chrome(chromedriver)
youtube_link = 'https://www.youtube.com/watch?v=somevideo'
driver.get(youtube_link)
这时,我可以time.wait()
表示视频的长度.
但是,我想知道是否可以通过Selenium驱动程序查询YouTube页面并在while循环中估算播放的剩余时间(我不熟悉如何从youtube页面提取此信息)>
谢谢!
[使用解决方案进行编辑]
硒解决方案
感谢Stanjer,并感谢此答案和 (不要忘记在脚本的开头添加"return") 硒替代解决方案 不太优雅,但值得指出:
[edit]根据Jose的评论,也可以通过以下方式访问此信息: 并发症和下一个问题 我需要以自动播放的最大化格式打开视频. 这意味着我调用了以下网址: 但是,这将返回一个非常短的html,其中仅包含嵌入代码,如此处的示例所示: 所以我在这里没有 方法1-我可以从 方法2-如果我在经典的YouTube页面上启动youtube视频,该如何告诉 (注意:此答案和此答案可能包含解决方法2的信息,如果我知道与Selenium一起使用) 您可以在youtube视频页面的上下文中执行javascript API: 因此,根据 https://developers.google.com/youtube/js_api_reference?csw = 1 您可以在循环中添加一个执行程序,每N秒检查一次状态. [Edit: solved, see mid-way in text of question] Quick question on understanding the contents of a Youtube page with a video playing in it: In summary I use Selenium to play videos on Youtube, as part of a browser-based GUI. I need to know when the video has finished playing, for the GUI to do something else (e.g. feed local HTML into the browser). Code snippet and question At this point, I could However, I wonder if I could query the YouTube page via the Selenium driver and gauge the time remaining for the play-through in a while loop (I am not familiar with how to extract this info from a youtube page) Thanks! [Edits with solution] Selenium Solution Thanks to Stanjer, and to this answer and this other answer, you can get the (don't forget to add "return" at the beginning of the script) Selenium Alternative Solution Less elegant but worth pointing out:
[edit] As per Jose's comment, this info can also be accessed by: Complication and next question I need to open up the video in maximised format, and with autoplay. This means I call the following url: However this returns a very short html which only contains an embed code, as per example here: So I have no Approach 1 - can I extract the timer from the Approach 2 - If I launch the youtube video in a classic Youtube page, how can I tell the (note: this answer and this answer probably contain info to solve approach 2, will post if I get this to work with Selenium) You can execute javascript API in context of youtube video page: So according to https://developers.google.com/youtube/js_api_reference?csw=1 You can add an executor in a loop checking the state every N seconds. 这篇关于Youtube + Selenium ChromeDriver(Python)-如何知道视频何时结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!driver.text
以此字符串格式"1:00/2:00"返回代表视频计时器的字符串.因此,您可以按照以下方式检查视频是否已通过播放:video_is_playing = True
while video_is_playing:
time.sleep(1)
video_is_playing = not(driver.text[:4] == driver.text[-4:])
driver.find_element_by_class_name("ytp-time-current").text
youtube.com/v/<video_code>?rel=0&autoplay=1
<HTML><HEAD></HEAD>
<BODY leftMargin=0 scroll=no topMargin=0>
<EMBED height="100%"
type=application/x-shockwave-flash
width="100%"
src=https://www.youtube.com/v/Fsc-oT9PsSQ?rel=0&autoplay=1
fullscreen="yes">
</BODY>
</HTML>
movie_player
元素. application/x-shockwave-flash
中提取计时器吗?movie_player
使其自身最大化?youtubePlayer = document.getElementById("movie_player");
youtubePlayer.getPlayerState();
state == 0
是视频结束时import os, time
from selenium import webdriver
# [...]
chromedriver = 'path_to_chromedriver_binary' # substitute as appropriate
driver = webdriver.Chrome(chromedriver)
youtube_link = 'https://www.youtube.com/watch?v=somevideo'
driver.get(youtube_link)
time.wait()
for the length of the video.movie_player
status through this method:player_status = driver.execute_script("return document.getElementById('movie_player').getPlayerState()")
driver.text
returns a string representing the video timer in this string format '1:00 / 2:00'. Therefore you can check if the video has played through by something along these lines:video_is_playing = True
while video_is_playing:
time.sleep(1)
video_is_playing = not(driver.text[:4] == driver.text[-4:])
driver.find_element_by_class_name("ytp-time-current").text
youtube.com/v/<video_code>?rel=0&autoplay=1
<HTML><HEAD></HEAD>
<BODY leftMargin=0 scroll=no topMargin=0>
<EMBED height="100%"
type=application/x-shockwave-flash
width="100%"
src=https://www.youtube.com/v/Fsc-oT9PsSQ?rel=0&autoplay=1
fullscreen="yes">
</BODY>
</HTML>
movie_player
element here. application/x-shockwave-flash
?movie_player
to maximise itself?youtubePlayer = document.getElementById("movie_player");
youtubePlayer.getPlayerState();
state == 0
is when a video has ended