模仿在Python中通过Selenium使用的PhantomJS上的HTML5视频支持 [英] Mimicking HTML5 Video support on PhantomJS used through Selenium in Python

查看:146
本文介绍了模仿在Python中通过Selenium使用的PhantomJS上的HTML5视频支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取在video标签中找到的HTML5视频的源链接.使用Firefox webdrive,我可以得到想要的结果,即-

I am trying to extract the source link of an HTML5 video found in the video tag . Using Firefox webdrive , I am able to get the desired result ie -

[<video class="video-stream html5-main-video" src='myvideoURL..'</video>]

但是如果我使用PhantomJS-

but if I use PhantomJS -

 <video class="video-stream html5-main-video" style="width: 854px; height: 480px; left: 0px; top: 0px; -webkit-transform: none;" tabindex="-1"></video>

我怀疑这是因为PhantomJS缺少HTML5视频支持.无论如何,我是否可以欺骗该网页,以为支持HTML5视频,以便它生成URL?还是我可以做其他事情?

I suspect this is because of PhantomJS' lack of HTML5 Video support . Is there anyway I can trick the webpage into thinking that HTML5 Video is supported so that it generates the URL ? Or can I do something else ?

尝试了

try:

    WebDriverWait(browser,10).until(EC.presence_of_element_located((By.XPATH, "//video")))


finally:


    k = browser.page_source


    browser.quit()


soup = BeautifulSoup(k,'html.parser')


print (soup.find_all('video'))

推荐答案

Firefox和phantomjs网络驱动程序与Selenium通信的方式完全不同.

The way Firefox and phantomjs webdrivers communicate with Selenium are quite different.

使用Firefox时,它会向您发送信号,表明页面已在加载了某些JavaScript后完成加载

When using Firefox, it signals back that the page has finished loading after it loaded some of the javascript

与phantomjs不同,它会向Selenium发出信号,表明只要页面能够获取页面源就意味着页面已完成加载,这意味着它不会加载任何JavaScript.

Differently in phantomjs, it signals Selenium that the page has finished loading as soon as it is able to get the page source meaning it wouldn't have loaded any javascript.

您需要做的是等待提取之前要存在的元素它,在这种情况下将是:

What you need to do is Wait for the element to be present before extracting it, in this case it would be:

video = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//video")))

Youtube在决定是否提供源之前先检查浏览器是否支持视频内容,尽管有描述,但有一种解决方法

Youtube first checks if the browser supports the video content before deciding whether to provide the source, theres a workaround though described here

这篇关于模仿在Python中通过Selenium使用的PhantomJS上的HTML5视频支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆