b站评论爬取

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;
}
posted @ 2018-12-26 21:33  公众号python学习开发  阅读(941)  评论(1编辑  收藏  举报