菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

使用油猴脚本获取抖音视频JSON数据

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:js教程 油猴脚本

油猴脚本是一种用于浏览器的脚本工具,可以方便地获取网页上的数据。要使用油猴脚本获取抖音视频JSON数据,首先需要安装Tampermonkey插件。然后,编写一个脚本,通过访问抖音视频的API接口,获取视频的JSON数据。这些数据可能包括视频的标题、描述、作者、点赞数等。最后,将获取到的数据进行处理和分析,以满足你的需求。需要注意的是,这种方法可能会受到抖音网站的限制,因此在使用前请确保遵守相关法律法规。

脚本介绍

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

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

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

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

油猴脚本代码

//==UserScript==//@name抖音用户主页抓取//@namespacehttp://tampermonkey.net///@version0.1//@descriptiontrytotakeovertheworld!//@authorYou//@matchhttps://www.douyin.com/user/*//@iconhttps://lf1-cdn-tos.bytegoofy.com/goofy/ies/douyin_web/public/favicon.ico//@grantnone//==/UserScript==(function(){'usestrict';varisFirst=true;varaweme_list=[];varnickname="";functionextractDataFromScript(){varscriptTag=document.getElementById('RENDER_DATA');if(!scriptTag)return;varencodedContent=scriptTag.innerHTML;vardecodedContent=decodeURIComponent(encodedContent);varjson=JSON.parse(decodedContent);for(varpropinjson){if(json.hasOwnProperty(prop)&&prop!=="_location"&&prop!=="app"){varuser=json[prop];nickname=user.user.user.nickname;varpost=user.post;vardata=post.data;aweme_list=aweme_list.concat(data);}}}functioncreateButton(){constbutton=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);}functionbuttonClick(){console.log(aweme_list);constfiles=[];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});}}elseif(item.aweme_type===68||item.awemeType===68){varurlList=item.images.map(img=>{try{returnimg.url_list[0];}catch(error){returnimg.urlList[0];}});files.push({name:item.desc,urlList:urlList});}});vardata={nickname:nickname,aweme_list:files};console.log(data);}functioninterceptResponse(){varoriginalSend=XMLHttpRequest.prototype.send;XMLHttpRequest.prototype.send=function(){varself=this;this.onreadystatechange=function(){if(self.readyState===4){if(self._url.indexOf("/aweme/v1/web/aweme/post")>-1){varjson=JSON.parse(self.response);vardata=json.aweme_list;aweme_list=aweme_list.concat(data);}}};originalSend.apply(this,arguments);};}functionscrollPageToBottom(){constSCROLL_DELAY=1000;//Adjustthedelaybetweeneachscrollaction(inmilliseconds)letscrollInterval;functiongetScrollPosition(){returnwindow.scrollY||window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0;}functionscrollToBottom(){window.scrollTo(0,document.body.scrollHeight);}functionhasReachedBottom(){returngetScrollPosition()>=(document.body.scrollHeight-window.innerHeight);}functionscrollLoop(){if(!hasReachedBottom()){scrollToBottom();}else{console.log("Reachedthebottomofthepage!");clearInterval(scrollInterval);//Youcanperformadditionalactionshereafterreachingthebottomofthepage.}}functionstartScrolling(){scrollInterval=setInterval(scrollLoop,SCROLL_DELAY);}functioncreateButton(){constbutton=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();}//Tostartscrolling,callthefunction:scrollPageToBottom();if(isFirst){console.log("首次加载");isFirst=false;setTimeout(function(){extractDataFromScript();createButton();},5000);//延迟时间为5000毫秒(即5秒)}interceptResponse();})();


评论
建站知识
建站知识
使用技巧
调试安装
运营推广