利用 PhantomJS 获取抖音直播的直播源

用手机把直播分享,把直播链接URL带进参数

PhantomJS.exe DYLive.js [直播URL]

如果获取成功返回m3u8格式的地址以及姓名和ID

var page = require('webpage').create(),
system = require('system'),
address;
if (system.args.length === 1) {
    console.log('Usage: DYLiveURL.js [URL]');
    phantom.exit();
}

address = system.args[1];

page.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25';

page.viewportSize = {
    width: 540,
    height: 960
};

page.open(address, function (status) {
    if (status === "success") {
        var ret = page.evaluate(function () {
                var userName = document.querySelector('.user-name').innerHTML;
                var emojiReg = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/gi;
                userName = userName.replace(emojiReg,'');

                var userID = document.querySelector('.info-id').innerHTML;
                var regex = /[0-9a-zA-Z_.]{4,}/gi;
                userID =userID.match(regex);
                var video = document.querySelectorAll('#theVideo');
                if (video.length == 0)
                    return 0;
                else
                    return video[0].outerHTML + ' LIVE_UN' + userName + 'LIVE_UN' +
                                                ' LIVE_ID' + userID + 'LIVE_ID';
            });
        if (ret == 0)
            console.log('NotFind');
        else {
            var regex = /(https|http).*?(?=\"|\"\)|\))/gi;
            var liveURL = ret.match(regex);
                regex = /(LIVE_UN).*?(LIVE_UN)/gi;
            var userName = ret.match(regex);
                regex = /(LIVE_ID).*?(LIVE_ID)/gi;
            var userID = ret.match(regex);

            console.log(liveURL);
            console.log(userName);
            console.log(userID);
        }
    }
    phantom.exit();
});

以上是2020年07月08日之前有效

下面是2020年07月08日抖音更新之后有效的代码

var page = require('webpage').create(),
system = require('system'),
address;
if (system.args.length === 1) {
    console.log('Usage: DYLiveURL.js [URL]');
    phantom.exit();
}

address = system.args[1];

page.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25';

page.viewportSize = {
    width: 540,
    height: 960
};

page.open(address, function (status) {
    if (status === "success") {
        var ret = page.evaluate(function () {
                var userName = document.querySelector('.name-wrap').innerHTML;
                var emojiReg = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030|(\ud83c[\udf00-\udfff])|(\ud83d[\udc00-\ude4f])|(\ud83d[\ude80-\udeff])|\ufe0f/gi;
                userName = userName.replace(emojiReg,'');

                var userID = document.querySelector('.uid').innerHTML;
                var regex = /[0-9a-zA-Z_.]{4,}/gi;
                userID =userID.match(regex);
                var video = document.querySelectorAll('.video');
                if (video.length == 0)
                    return 0;
                else
                    return video[0].outerHTML + ' LIVE_UN' + userName + 'LIVE_UN' +
                                                ' LIVE_ID' + userID + 'LIVE_ID';
            });
        if (ret == 0)
            console.log('NotFind');
        else {
            var regex = /(https|http).*?(?=\"|\"\)|\))/gi;
            var liveURL = ret.match(regex);
                regex = /(LIVE_UN).*?(LIVE_UN)/gi;
            var userName = ret.match(regex);
                regex = /(LIVE_ID).*?(LIVE_ID)/gi;
            var userID = ret.match(regex);

            console.log(liveURL);
            console.log(userName);
            console.log(userID);
        }
    }
    phantom.exit();
});

 

posted @ 2020-07-07 23:25  1CM  阅读(1917)  评论(0编辑  收藏  举报