static void GetCompleted(int state, List <UserInfoModel> list) { //通知完成 Console.WriteLine($"[{pageindex}/{totalpage}] GetCompleted:" + list.Count); //保存数据 data.AddRange(list); File.WriteAllText(savename, JsonConvert.SerializeObject(data)); //判断是否还有数据 pageindex++; if (pageindex <= totalpage) { //还有数据 ulist = WeiboHelper.GetPageLikeLinks(mid, pageindex); GetWeiboUser getWeiboUser = new GetWeiboUser(ulist.UserLinks, 30); getWeiboUser.GetCompleted += GetCompleted; getWeiboUser.Get(); } else { timer.Change(Timeout.Infinite, 1000); //没有 Console.WriteLine($"抓取已完成,耗时 {time} 秒。总计 {totalpage} 页数据,抓取到用户 {data.Count} 位。"); File.WriteAllText(savename, JsonConvert.SerializeObject(data)); Console.WriteLine($"数据保存在程序启动目录:{savename}"); time = 0; pageindex = 1; data.Clear(); } }
static void Main(string[] args) { Console.Title = "WeiboLike"; timer = new Timer(new TimerCallback(TimerUp), null, Timeout.Infinite, 1000); while (true) { //Console.WriteLine("请输入用户ID:"); //string uid = Console.ReadLine(); //if (string.IsNullOrEmpty(uid)) //{ // continue; //} //Console.WriteLine("请输入微博ID:"); //string weiboid = Console.ReadLine(); //if (string.IsNullOrEmpty(weiboid)) //{ // continue; //} Console.WriteLine("请粘贴微博URL链接:"); string[] uidweiboid = WeiboHelper.GetUidWeiboID(Console.ReadLine()); if (string.IsNullOrEmpty(uidweiboid[0]) || string.IsNullOrEmpty(uidweiboid[1])) { Console.WriteLine("从链接获取uid/微博id失败,请重试。"); continue; } Console.WriteLine("正在获取Mid..."); mid = WeiboHelper.GetMid(uidweiboid[0], uidweiboid[1]); if (string.IsNullOrEmpty(mid)) { Console.WriteLine("没有获取到Mid,请重试。"); continue; } Console.WriteLine("已获取到MID:" + mid); Console.WriteLine("请输入微博数据保存文件名:"); savename = Console.ReadLine() + ".json"; Console.WriteLine("开始抓取本条微博的点赞用户数据..."); timer.Change(0, 1000); //第一步获取点赞基本数据 ulist = WeiboHelper.GetPageLikeLinks(mid, 1); totalpage = ulist.TotalPage; GetWeiboUser getWeiboUser = new GetWeiboUser(ulist.UserLinks, 60); getWeiboUser.GetCompleted += GetCompleted; getWeiboUser.Get(); Console.ReadKey(); } }