python selenium 播放视频_模仿通过Selenium在Python中使用的PhantomJS对HTML5视频的支持...

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 -

[]

but if I use PhantomJS -

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 ?

tried this

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'))

解决方案

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

When using Firefox, it signals back that the page has finished loading after it loaded some of the 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")))

EDIT:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值