JS實(shí)現(xiàn)b站動(dòng)態(tài)評(píng)論區(qū)抽獎(jiǎng)(含去重)

新前言?。?!?
因?yàn)閎站新版本動(dòng)態(tài)評(píng)論區(qū)頁(yè)碼取消,改為下滑加載的原因,舊版本代碼已經(jīng)無(wú)法使用,需要使用新版本3.0代碼,才能正常抽取,使用方法一樣,代碼在文章底部更新。
補(bǔ)充
當(dāng)然 感興趣的可以去試試官方API?
https://api.bilibili.com/x/v2/reply/main?callback=jQuery33106548244954033764_1618553940827&jsonp=jsonp&next=4&type=11&oid=130267145&mode=3&_=1618553940831
我這也簡(jiǎn)單分析一下





video

前期
測(cè)試頁(yè)面鏈接:https://t.bilibili.com/394309046095520212?tab=2
動(dòng)態(tài)評(píng)論數(shù)量為226,這個(gè)數(shù)量是全樓層的評(píng)論數(shù)量

共分為8頁(yè)展示,一頁(yè)首層人數(shù)20人

教程
1、訪問頁(yè)面
PC端打開瀏覽器,訪問你的動(dòng)態(tài)抽獎(jiǎng)頁(yè)面。點(diǎn)在評(píng)論上,不用點(diǎn)轉(zhuǎn)發(fā)。

2、打開“檢查”
鼠標(biāo)右鍵,打開“檢查”,一般的快捷鍵都是F12

長(zhǎng)這樣,我們需要在console(控制臺(tái))下輸入代碼。
3、貼入代碼
超人性化設(shè)計(jì),一步到位,還會(huì)打印所有用戶名,如果不需要可以注釋掉打印代碼,就是下面那些console.log代碼,注釋使用 //?,不會(huì)的可以自行百度JS注釋。
console.log("程序開始運(yùn)行");
console.log("定義集合存儲(chǔ)數(shù)據(jù)");
let name_set = new Set();
let id_set = new Set();
console.log("翻到第一頁(yè)");
document.getElementsByClassName("tcd-number")[0].click();
document.getElementsByClassName("tcd-number")[0].click();
console.log("開始載入數(shù)據(jù)");
var page = 1;
var my_loop;
// 抽獎(jiǎng)函數(shù)
function draw()
{
? ? // 循環(huán)次數(shù)
????for(var i=0; i<document.getElementsByClassName("con").length; i++)
????{
????????var name = document.getElementsByClassName("con")[i].getElementsByClassName("user")[0].getElementsByTagName("a")[0].innerText;
????????console.log(name+",加入集合");
????????name_set.add(name);
????????var id = document.getElementsByClassName("con")[i].getElementsByClassName("user")[0].getElementsByTagName("a")[0].getAttributeNode("data-usercard-mid").value;
????????id_set.add(id);
????}
???? console.log("第"+page+"頁(yè)數(shù)據(jù)存入Set完畢");
???? if(null != document.getElementsByClassName("next")[0])
???? {
???? ????page++;
???? ????console.log("自動(dòng)翻頁(yè)...");
???? ????document.getElementsByClassName("next")[0].click();
???? ????//return true;
???? }
???? else
???? {
???? ????console.log("全部數(shù)據(jù)加載完畢");
???? ????console.log("總共"+name_set.size+"名用戶");
???? ????// 生成隨機(jī)數(shù),直接打印中獎(jiǎng)?wù)咝畔?/span>
???? ????var lucky_num = parseInt(Math.random()*(name_set.size),10);
???? ????console.log("中獎(jiǎng)用戶ID為:"+Array.from(id_set)[lucky_num]);
???? ????console.log("中獎(jiǎng)用戶名為:"+Array.from(name_set)[lucky_num]);
???? ????console.log("中獎(jiǎng)?wù)叽蟾盼挥?第" +parseInt(lucky_num/20+1)+ "頁(yè)");
???? ????clearInterval(my_loop);
???? ????// 這就是注釋
???? ????//return false;
???? }
}
// 定時(shí)調(diào)用函數(shù)
my_loop = setInterval(draw, 1500);
實(shí)際運(yùn)行測(cè)試如下,如果評(píng)論人數(shù)較多或者網(wǎng)速較慢,請(qǐng)修改最后一行 my_loop = setInterval(draw, 1000);?把這里的1000調(diào)大,1000代表1s,這是自動(dòng)翻頁(yè)的時(shí)間。

10頁(yè)數(shù)據(jù)加載完畢后,這就是中獎(jiǎng)?wù)咝畔?br>

3.0?新版本b站?單人版
使用方法相同,選中評(píng)論區(qū)后,console中貼入代碼即可
console.log("程序開始運(yùn)行");
console.log("定義集合存儲(chǔ)數(shù)據(jù)");
let name_set = new Set();
let id_set = new Set();
console.log("開始載入數(shù)據(jù)");
// 循環(huán)變量
var my_loop;
// 下滑延時(shí) 500毫秒 網(wǎng)速/加載速度較慢的朋友們最好放慢速度 提高準(zhǔn)確性
var r_time = 500;
// 評(píng)論數(shù)
var comment_num = parseInt(document.getElementsByClassName("text-offset")[1].innerText);
// 下滑
function r()
{
window.scroll(0, 1920*comment_num);
// 沒有評(píng)論后自動(dòng)停止下滑 并 抽獎(jiǎng)
if(document.getElementsByClassName("loading-state")[0].innerText == "沒有更多評(píng)論")
{
// 停止下滑循環(huán)
stop_r();
// 抽獎(jiǎng)函數(shù)
draw();
}
}
// 停止下滑循環(huán)
function stop_r()
{
clearInterval(my_loop);
}
// 抽獎(jiǎng)函數(shù)
function draw()
{
? ? // 循環(huán)次數(shù)
? ? var len = document.getElementsByClassName("con").length;
for(var i=0; i<len; i++)
{
var name = document.getElementsByClassName("con")[i].getElementsByClassName("user")[0].getElementsByTagName("a")[0].innerText;
// console.log(name+",加入集合");
name_set.add(name);
var id = document.getElementsByClassName("con")[i].getElementsByClassName("user")[0].getElementsByTagName("a")[0].getAttributeNode("data-usercard-mid").value;
id_set.add(id);
}
console.log("全部數(shù)據(jù)加載完畢");
console.log("總共"+name_set.size+"名用戶");
// 生成隨機(jī)數(shù),直接打印中獎(jiǎng)?wù)咝畔?/span>
var lucky_num = parseInt(Math.random()*(name_set.size),10);
console.log("中獎(jiǎng)用戶ID為:"+Array.from(id_set)[lucky_num]);
console.log("中獎(jiǎng)用戶名為:"+Array.from(name_set)[lucky_num]);
// 這就是注釋
//return false;
}
// 開始自動(dòng)下滑 r_time毫秒一次
my_loop = setInterval(r, r_time);
3.1新版本多人抽取(適應(yīng)b站動(dòng)態(tài)改動(dòng))
數(shù)據(jù)加載完畢后,使用 go(中獎(jiǎng)人數(shù)) 即可抽取
console.log("程序開始運(yùn)行");
console.log("定義集合存儲(chǔ)數(shù)據(jù)");
let name_set = new Set();
let id_set = new Set();
console.log("開始載入數(shù)據(jù)");
// 循環(huán)變量
var my_loop;
// 下滑延時(shí) 500毫秒 網(wǎng)速/加載速度較慢的朋友們最好放慢速度 提高準(zhǔn)確性
var r_time = 500;
// 評(píng)論數(shù)
var comment_num = 1;
if(document.getElementsByClassName("text-offset")[1].innerText.indexOf("萬(wàn)") != -1)
{
comment_num = 10000 * (parseInt(document.getElementsByClassName("text-offset")[1].innerText) + 1);
}
else
{
comment_num = parseInt(document.getElementsByClassName("text-offset")[1].innerText);
}
// 下滑
function r()
{
window.scroll(0, 1920*comment_num);
// 沒有評(píng)論后自動(dòng)停止下滑 并 抽獎(jiǎng)
if(document.getElementsByClassName("loading-state")[0].innerText == "沒有更多評(píng)論")
{
// 停止下滑循環(huán)
stop_r();
// 抽獎(jiǎng)函數(shù)
draw();
}
}
// 停止下滑循環(huán)
function stop_r()
{
clearInterval(my_loop);
}
// 抽獎(jiǎng)函數(shù)
function draw()
{
? ? // 循環(huán)次數(shù)
? ? var len = document.getElementsByClassName("con").length;
for(var i=0; i<len; i++)
{
var name = document.getElementsByClassName("con")[i].getElementsByClassName("user")[0].getElementsByTagName("a")[0].innerText;
// console.log(name+",加入集合");
name_set.add(name);
var id = document.getElementsByClassName("con")[i].getElementsByClassName("user")[0].getElementsByTagName("a")[0].getAttributeNode("data-usercard-mid").value;
id_set.add(id);
}
console.log("全部數(shù)據(jù)加載完畢");
console.log("總共"+name_set.size+"名用戶");
// 這就是注釋
//return false;
}
// 獲取幸運(yùn)兒
function go(num)
{
for(var i=0; i<num; i++)
{
// 生成隨機(jī)數(shù),直接打印中獎(jiǎng)?wù)咝畔?/span>
var lucky_num = parseInt(Math.random()*(name_set.size), 10);
console.log(" ");
console.log("中獎(jiǎng)用戶ID為:"+Array.from(id_set)[lucky_num]);
console.log("中獎(jiǎng)用戶名為:"+Array.from(name_set)[lucky_num]);
console.log(" ");
}
}
// 開始自動(dòng)下滑 r_time毫秒一次
my_loop = setInterval(r, r_time);
// 全部數(shù)據(jù)加載完畢后,使用 go(中獎(jiǎng)數(shù)) 抽取中獎(jiǎng)?wù)?/span>