码迷,mamicode.com
首页 > 其他好文 > 详细

b站评论爬取

时间:2018-12-26 22:19:29      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:rda   data   com   shuff   +=   list   ctime   cti   back   

var userData = {};
var startPage = 0;
var endPage = 0;
var startTime = "";
var endTime = "";
var pickSum = 0;
var dateItem = new Date();
function StartDraw(_startPage, _endPage, _startTime, _endTime, _pickSum)
{
  startPage = _startPage;
  endPage = _endPage;
  startTime = _startTime;
  endTime = _endTime;
  pickSum = _pickSum;
  GetUserData();
}
function GetUserData()
{
  GetOneData(startPage, endPage);
}
function callback(data)
{
}
function GetOneData(page, maxPage)
{
  urlLocationParams = window.location.pathname.split("/");
  avID = urlLocationParams[urlLocationParams.length - 1].split("av")[1];
  var url = "https://api.bilibili.com/x/v2/reply?callback=callback&jsonp=jsonp&pn=" + page + "&type=1&oid=" + avID + "&sort=0&_=" + dateItem.getTime();
  $.ajax({
    url: url,
    type: ‘get‘,
    dataType: ‘jsonp‘,
    jsonpCallback: ‘callback‘,
    success: function (data) {
      console.log(data);
      for (var j = 0; j < data.data.replies.length; j++) {
        var reply = data.data.replies[j];
        if (reply.ctime >= get_unix_time_stamp(startTime)
          && reply.ctime < get_unix_time_stamp(endTime)) {
          userData[reply.mid] = {};
          userData[reply.mid].uid = reply.mid;
          userData[reply.mid].floor = reply.floor;
          userData[reply.mid].name = reply.member.uname;
          userData[reply.mid].content = reply.content.message;
        }
      }
      page += 1;
      if (page <= maxPage)
      {
        GetOneData(page, maxPage)
      }
      else
      {
        PickUser();
      }
    }
  });
}
function PickUser()
{
  userList = [];
  for (var user in userData)
  {
    userList.push(user);
  }
  shuffle(userList);
  for (var i = 0; i < pickSum; i++)
  {
    console.log(userData[userList[i]])
  }
}
function get_unix_time_stamp(strtime) 
{
  if (strtime) {
    var date = new Date(strtime);
  } else {
    var date = new Date();
  }
  time = date.getTime() / 1000;
  return time;
}
function shuffle(array) {
  length = array.length;
  for (var index = 0; index < array.length; index++)
  {
    var temp = array[index];
    var changeIndex = RandomNumBoth(0, length - 1);
    array[index] = array[changeIndex];
    array[changeIndex] = temp;
  }
}
function RandomNumBoth(Min,Max){
  var Range = Max - Min;
  var Rand = Math.random();
  var num = Min + Math.round(Rand * Range); 
  return num;
}

b站评论爬取

标签:rda   data   com   shuff   +=   list   ctime   cti   back   

原文地址:https://www.cnblogs.com/c-x-a/p/10181969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!