js教程

油猴脚本获取抖音视频JOSN数据

我的站长站 2023-07-23 人阅读

脚本介绍

重写XMLHttpRequest的Send函数达到拦截。
注意,是用户主页,比如:https://www.douyin.com/user/MS4wLjABAAAAd4IEE9JOezbMuKOhRFAEAwlN3D5qgBDvTjjqV2g5FHM?is_search=0&list_name=follow&nt=0

页面5秒钟后会在页面右下角生成一个按钮,点击后会在控制台打印数据

油猴脚本获取抖音视频JOSN数据
油猴脚本截图

一个是原始数据,一个是经过处理的数据(标题+作品地址)

油猴脚本代码

// ==UserScript==
// @name         抖音用户主页抓取
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.douyin.com/user/*
// @icon         https://lf1-cdn-tos.bytegoofy.com/goofy/ies/douyin_web/public/favicon.ico
// @grant        none
// ==/UserScript==
 
(function() {
    'use strict';
    var isFirst = true;
    var aweme_list = [];
    var nickname = "";
 
    function extractDataFromScript() {
        var scriptTag = document.getElementById('RENDER_DATA');
        if (!scriptTag) return;
 
        var encodedContent = scriptTag.innerHTML;
        var decodedContent = decodeURIComponent(encodedContent);
        var json = JSON.parse(decodedContent);
 
        for (var prop in json) {
            if (json.hasOwnProperty(prop) && prop !== "_location" && prop !== "app") {
                var user = json[prop];
                nickname = user.user.user.nickname;
                var post = user.post;
                var data = post.data;
                aweme_list = aweme_list.concat(data);
            }
        }
    }
 
    function createButton() {
        const button = document.createElement('button');
        button.textContent = '点击我';
        button.style.position = 'fixed';
        button.style.right = '20px';
        button.style.bottom = '30%';
        button.addEventListener('click', buttonClick);
        document.body.appendChild(button);
    }
 
    function buttonClick() {
        console.log(aweme_list);
        const files = [];
        aweme_list.forEach((item) => {
            if (item.aweme_type === 0 || item.awemeType === 0 || item.aweme_type === 61 || item.awemeType === 61) {
                try {
                    files.push({ name: item.desc, url: item.video.play_addr.url_list[0] });
                } catch (error) {
                    files.push({ name: item.desc, url: item.video.playAddr[0].src });
                }
            } else if (item.aweme_type === 68 || item.awemeType === 68) {
                var urlList = item.images.map(img => {
                    try {
                        return img.url_list[0];
                    } catch (error) {
                        return img.urlList[0];
                    }
                });
                files.push({ name: item.desc, urlList: urlList });
            }
        });
 
        var data = { nickname: nickname, aweme_list: files };
        console.log(data);
    }
 
    function interceptResponse() {
        var originalSend = XMLHttpRequest.prototype.send;
        XMLHttpRequest.prototype.send = function() {
            var self = this;
            this.onreadystatechange = function() {
                if (self.readyState === 4) {
                    if (self._url.indexOf("/aweme/v1/web/aweme/post") > -1) {
                        var json = JSON.parse(self.response);
                        var data = json.aweme_list;
                        aweme_list = aweme_list.concat(data);
                    }
                }
            };
            originalSend.apply(this, arguments);
        };
    }
    function scrollPageToBottom() {
        const SCROLL_DELAY = 1000; // Adjust the delay between each scroll action (in milliseconds)
        let scrollInterval;
 
        function getScrollPosition() {
            return window.scrollY || window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
        }
 
        function scrollToBottom() {
            window.scrollTo(0, document.body.scrollHeight);
        }
 
        function hasReachedBottom() {
            return getScrollPosition() >= (document.body.scrollHeight - window.innerHeight);
        }
 
        function scrollLoop() {
            if (!hasReachedBottom()) {
                scrollToBottom();
            } else {
                console.log("Reached the bottom of the page!");
                clearInterval(scrollInterval);
                // You can perform additional actions here after reaching the bottom of the page.
            }
        }
 
        function startScrolling() {
            scrollInterval = setInterval(scrollLoop, SCROLL_DELAY);
        }
 
        function createButton() {
            const button = document.createElement('button');
            button.textContent = '点击开始下拉';
            button.style.position = 'fixed';
            button.style.right = '20px';
            button.style.bottom = '35%';
            button.addEventListener('click', startScrolling);
            document.body.appendChild(button);
        }
 
        createButton();
    }
 
    // To start scrolling, call the function:
    scrollPageToBottom();
 
 
    if (isFirst) {
        console.log("首次加载");
        isFirst = false;
        setTimeout(function() {
            extractDataFromScript();
            createButton();
        }, 5000); // 延迟时间为5000毫秒(即5秒)
    }
 
    interceptResponse();
 
})();


相关推荐
  • 油猴脚本
  • 利用油猴脚本突破百度网盘下载限速方法

    教程介绍分享一篇自用利用油猴脚本突破百度网盘下载限速方法,手机安卓版,PC版现在已经很难破解了,下载可以达到svip会员效果。油猴脚本百度网盘不限速截图准备软件①Firefox Nightly②idm+③(Tampermonkey)油猴④软件小妹的脚本《百度网盘简易下载助手》...

    经验分享 1762 2年前
  • 油猴脚本去除csdn登录才能复制限制
    油猴脚本去除csdn登录才能复制限制

    作为程序猿,应该会经常去csdn参(chao)考(xi)代码,今天在复制一篇文章的代码的时候,突然发现需要登录才能复制,但是我用github授权登录的时候居然失败了!突发奇想,写个脚本解除这个限制吧!稍微看一下文档结构...

    浏览器插件 134 2年前
  • 百度网盘提取码自动填写油猴脚本
    百度网盘提取码自动填写油猴脚本

    插件介绍百度网盘提取码自动填写油猴脚本,新上线的网盘油猴脚本,它可以实现识别网页中的网盘提取码并自动填写的功能。这款油猴脚本支持自动填充百度云、360盘等的提取码,还额外带有淘宝天猫找优惠...

    浏览器插件 694 3年前
最新更新
  • js截取字符串教程

    slice()方法接受两个参数,起始索引和结束索引(可选)。它返回从起始索引到结束索引(不包括结束索引)之间的子字符...

    js教程 3个月前
  • find findIndex indexOf索引选择器使用方法

    find使用方法find方法是ES6引入的一种数组方法,可以用来查找数组中符合条件的元素。语法是:array.find(callba...

    js教程 3个月前
  • js复制网页内容教程

    Async Clipboard API方法HTML5新增的方法,无需引入第三方插件,直接就可以复制内容。低版本的浏览器可能会不兼...

    js教程 3个月前
  • js获取字符长度函数分享

    js获取字符长度函数function objLen(str) { if (str == null) return 0; if (typeof str != "string") { ...

    js教程 3个月前
  • 网站LED跑马灯效果广告代码

    网站可以看到很多的论坛网站都会用到这种网站LED跑马灯效果,这种效果实现也很简单,分享给大家。LED跑马灯效果...

    js教程 4个月前