使用python下载嵌入式视频

时间:2016-10-29 18:00:42

标签: python video web-scraping

我正在尝试通过python下载嵌入式链接,下面是一个示例链接

https://matterhorn.dce.harvard.edu/engage/player/watch.html?id=f7ff1893-fbf7-4909-b44e-12e61a98a677

当我导航到该页面时,需要加载一些并且还必须按播放,

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

如果您将查看生成的页面源(在加载DOM并运行javascript代码之后),您将看到它是一个HTML页面(而不是视频的链接)。源包含生成此html的javascript代码:

<div id="playerContainer_videoContainer_container" role="main" style="position: relative; display: block; margin-left: auto; margin-right: auto; width: 1902px; height: 1070px; top: 0px;">
<div id="overlayContainer" role="main" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; overflow: hidden; z-index: 10;"></div>
<img id="playerContainer_videoContainer_bkg" src="config/profiles/resources/slide_professor_paella.jpg" alt="" width="100%" height="100%" style="position: relative; top: 0px; left: 0px; right: 0px; bottom: 0px; z-index: 0;">
<video id="playerContainer_videoContainer_1" preload="auto" style="top: 18.4722%; left: 0.390625%; width: 65%; height: 65%; position: absolute; z-index: 1;" poster="https://da4w749qm6awt.cloudfront.net/engage-player/f7ff1893-fbf7-4909-b44e-12e61a98a677/attachment-5/presenter_delivery.jpg">
    <source src="https://da4w749qm6awt.cloudfront.net/engage-player/f7ff1893-fbf7-4909-b44e-12e61a98a677/24320288-b79e-49e5-93b6-96b4c208f8cb/presenter_delivery.mp4" type="video/mp4">
</video>
<video id="playerContainer_videoContainer_2" preload="auto" style="top: 33.4722%; left: 66.0156%; width: 33.75%; height: 33.75%; position: absolute; z-index: 1;" poster="https://da4w749qm6awt.cloudfront.net/engage-player/f7ff1893-fbf7-4909-b44e-12e61a98a677/attachment-8/presentation_delivery.jpg">
    <source src="https://da4w749qm6awt.cloudfront.net/engage-player/f7ff1893-fbf7-4909-b44e-12e61a98a677/93271e20-3f4b-4650-a7e3-95aac41fd3e5/presentation_delivery.mp4" type="video/mp4">
</video>

因此,您实际要下载的文件是

https://da4w749qm6awt.cloudfront.net/engage-player/f7ff1893-fbf7-4909-b44e-12e61a98a677/24320288-b79e-49e5-93b6-96b4c208f8cb/presenter_delivery.mp4"

如果您检查network标签(在开发人员工具栏中),您会发现对此网址的ajax请求:

https://matterhorn.dce.harvard.edu/search/episode.json?id=f7ff1893-fbf7-4909-b44e-12e61a98a677&_=1477764682940

(您可以看到id此处与原始网址中的ID相同。)

此请求的响应是json字符串:

{"search-results":{"searchTime":"1","total":"1","limit":"1","offset":"0","query":"(id:f7ff1893\\-fbf7\\-4909\\-b44e\\-12e61a98a677) AND oc_organization:mh_default_org AND (o
  

只有部分回应,因为它太大而无法放在这里。

部分回复是:

search-results.result.mediapackage.media.track

其中有6个项目,每个项目都有一个可用于获取相关视频链接的网址属性:

enter image description here

我认为这些信息为您提供了一个好的起点:)