首页 编程教程正文

python爬虫 抖音短视频解析下载

piaodoo 编程教程 2020-02-22 22:02:28 883 0 python教程

本文来源吾爱破解论坛

今天无意间看到http://douyin.iiilab.com/可以解析下载抖音短视频,闲着没事就调用它的接口写了个解析的
[Python] 纯文本查看 复制代码

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)

if __name__ == '__main__':
    # 第一次访问的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)

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/7389.html

搜索