1024程序员节 - 分享一个抖音视频下载程序

版权声明: https://blog.csdn.net/qq_26877377/article/details/83344463

在网上调用别人的接口来实现的功能 

import requests
import execjs


# 生成参数s
def generateStr(a):
    js = '''
     test = function(a) {
                var c = function() {
                     for (var d = 0,
                     f = new Array(256), g = 0; 256 != g; ++g) {
                          d = g,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          f[g] = d
                     }
                     return "undefined" != typeof Int32Array ? new Int32Array(f) : f
                } (),
                b = function(g) {
                     for (var j, k, h = -1,
                     f = 0,
                     d = g.length; f < d;) {
                          j = g.charCodeAt(f++),
                          j < 128 ? h = h >>> 8 ^ c[255 & (h ^ j)] : j < 2048 ? (h = h >>> 8 ^ c[255 & (h ^ (192 | j >> 6 & 31))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))]) : j >= 55296 && j < 57344 ? (j = (1023 & j) + 64, k = 1023 & g.charCodeAt(f++), h = h >>> 8 ^ c[255 & (h ^ (240 | j >> 8 & 7))], h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 2 & 63))], h = h >>> 8 ^ c[255 & (h ^ (128 | k >> 6 & 15 | (3 & j) << 4))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & k))]) : (h = h >>> 8 ^ c[255 & (h ^ (224 | j >> 12 & 15))], h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 6 & 63))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))])
                     }
                     return h ^ -1
                };
                return b(a) >>> 0
          }
     '''
    ctx = execjs.compile(js)
    return ctx.call('test', a)


def run():
    # 第一次访问的url(获取cookie)
    base_url = 'http://douyin.iiilab.com'

    # 解析url
    url = 'http://service0.iiilab.com/video/web/douyin'

    # link = 'http://v.douyin.com/d3pqo9/'
    # 抖音需要去水印的链接
    link = input('请输入链接\n')

    # r是随机数
    r = execjs.eval('Math.random().toString(10).substring(2)')

    # s是根据随机数r一定的规则生成的(太长不想分析, 直接用execjs这个库直接跑js代码)
    s = generateStr('{}@{}'.format(link, r))
    headers = {
        'Host': 'douyin.iiilab.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3514.0 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'Accept-Encoding': 'gzip,deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9'
    }
    data = {
        'link': link,
        'r': r,
        's': s
    }

    # 使用requests的session保留cookies
    sess = requests.Session()

    # 要加入headers,不然报错
    sess.headers.update(headers)

    # 模拟正常访问网页获取cookies
    res = sess.get(base_url)

    headers = {
        'Host': 'service0.iiilab.com',
        'Origin': 'http://douyin.iiilab.com',
        'Referer': 'http://douyin.iiilab.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3514.0 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'Accept-Encoding': 'gzip,deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9'
    }
    sess.headers.update(headers)

    # 获取返回的数据
    res = sess.post(url, data=data, headers=headers).json()

    if res['retCode'] == 200:
        print(res)
        # 封面
        cover = res['data']['cover']
        # 标题
        title = res['data']['text']
        # 去水印后的视频
        video = res['data']['video']
    else:
        print(res)


if __name__ == '__main__':
    run()

猜你喜欢

转载自blog.csdn.net/qq_26877377/article/details/83344463