美文网首页
记一次爬取使用aliplayer-min.js的视频

记一次爬取使用aliplayer-min.js的视频

作者: 洛洛尘沙 | 来源:发表于2019-06-21 17:49 被阅读0次

    aliplayer-min.js是阿里的web播放器SDK,封装了很多稀奇古怪的东西。

    经过了浏览源码、精简源码、修改源码的过程,建议把aliplayer-min.js当作黑盒使用,略微修改其中的代码把想要的结果输出即可。

    大致步骤

    spider_ali.png

    其他细节

    1.寻找使用aliplayer-min.js的蛛丝马迹

    通过阅读阿里web播放器SDK的文档,发现如果要使用该SDK,有一个固定的语法”var player = new Aliplayer({“,所以,燥起来,去找含有这个语法的js代码吧。如果没找到,说明藏得比较深,再找一次。

    2.构建本地模拟页面

    话不多说,直接上代码:

    <!DOCTYPE html>
    <html>
        <head>
         <meta charset="UTF-8">
         <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/>
         <title>AliPlayer Tester</title>
         <link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.8.2/skins/default/aliplayer-min.css" />
         <script charset="utf-8" type="text/javascript" src="./aliplayer-min-ly.js"></script>
        </head>
        <body>
            <div class="prism-player" id="J_prismPlayer"></div>
            <div id="get-video"></div>
            <script>
               var player = new Aliplayer({
               id: "J_prismPlayer",
               width: "100%",
               height: "100%",
               autoplay: true,
               vid: "fc-videoid",
               playauth: "fc-playauth",
               cover: "fc-cover",
               qualitySort: "desc",
               region:'cn-shanghai',
               });
            </script>
        </body>
    </html>
    

    注意:aliplayer-min-ly.js是我修改后的版本,目的是把对应接口URL暴露出来。具体修改部分如下:

    var r = u.makeUTF8sort(i, "=", "&") + "&Signature=" + u.AliyunEncodeURI(u.makeChangeSiga(i, e.accessSecret)),
        n = "https://vod." + e.domainRegion + ".aliyuncs.com/?" + r;
        console.log("88:" + n);  // 修改部分,增加console打印
        document.getElementById("get-video").innerHTML=n; // 修改部分,结果体现到页面,方面后面抓取
    // l.get(n, function (e) {
    //         if (e) {
    //             var t = JSON.parse(e),
    // ... 省略
    

    3.Python + Selenium + PhantomJS请求接口

    不断替换构建的本地页面中的参数,用Selenium + PhantomJS调用修改的aliplayer-min-ly.js获取想要的接口URL,通过该接口URL最终获得视频的地址。

    相关代码如下:

    # 模拟构建请求页面
    with open("/Users/song/Desktop/aliplayer-fc.html", "r") as f:
        fc_page = f.read()
    with open("/Users/song/Desktop/aliplayer-fc-fc.html", "w") as f:
        f.write(fc_page.replace("fc-videoid", courseModuleRecordVideoId).replace("fc-playauth", play_playAuth).replace("fc-cover", play_cover))
    browser.get('file:///Users/song/Desktop/aliplayer-fc-fc.html')
    get_video_url = browser.find_element_by_id("get-video").text
    video_info_response = opener.open(get_video_url)
    video_info_json = json.loads(video_info_response.read(), encoding="UTF-8")
    mp4_video_url = video_info_json['PlayInfoList']['PlayInfo'][1]['PlayURL']
    mp4_video_duration = video_info_json['PlayInfoList']['PlayInfo'][1]['Duration']
    os.makedirs(base_name + "/" + courseModuleRecordName, exist_ok=True)
    full_video_name = base_name + "/" + courseModuleRecordName + "/" + courseModuleRecordDetailName
    print(full_video_name, mp4_video_url, mp4_video_duration)
    video_downloader(mp4_video_url, full_video_name)
    

    下载视频方法:

    # 下载视频函数
    def video_downloader(video_url, video_name):
        print("开始下载【" + video_name + "】,URL:" + video_url)
        video_streams = requests.get(video_url, stream=True)
        video_format = re.sub("\?.*", "", re.sub(".*\.", "", video_url))
        v_name = video_name + "." + video_format
        with open(v_name, "wb") as video:
            for chunk in video_streams.iter_content(chunk_size=1024*1024):
                if chunk:
                    video.write(chunk)
        print("FINISHED!")
    

    相关文章

      网友评论

          本文标题:记一次爬取使用aliplayer-min.js的视频

          本文链接:https://www.haomeiwen.com/subject/leyuqctx.html