private void Timer_Elapsed(object sender, ElapsedEventArgs e) { var r = http.getHtml("http://www.huya.com/bashen"); var rr = new rec { timesamp = ConvertDateTimeInt(DateTime.Now), roomid = roomid, activitycount = Between(r, "<div id=\"activityCount\">", "<"), livecount = Between(r, "id=\"live-count\">", "<") }; //写入数据库 Form1.Cmd( $"INSERT into rec (roomname,timesamp,livecount,activityCount)values('{rr.roomid}','{rr.timesamp}','{rr.livecount}','{rr.activitycount}')"); Console.Out.WriteLine("done"); }
public void getallup() { lock (logger) { var sw = new Stopwatch(); sw.Start(); var swr = sw.ElapsedMilliseconds; var alluphtml = http.getHtml("http://www.huya.com/g/hw"); //logger.Info("getallhtml time={0}", sw.ElapsedMilliseconds - swr); var re = new Regex("game-live-item\">(\\s*?)<a href=\"([\\s\\S]*?)\" class=\"([\\s\\S]*?)' title=\"([\\s\\S]*?)\" target=\"([\\s\\S]*?)nick\" title=\"([\\s\\S]*?)\">([\\s\\S]*?)\"js-num\">([\\s\\S]*?)<"); var ms = re.Matches(alluphtml); //2 4 6 8 // logger.Info("ms={0}", ms.Count); var ts = Spider.ConvertDateTimeInt(DateTime.Now); var totalcount = 0; var rs = new List <rec>(); foreach (Match m in ms) { var url = m.Groups[2].ToString(); url += "*"; var r = new rec() { roomid = Spider.Between(url, "com/", "*"), title = m.Groups[4].ToString(), nick = m.Groups[6].ToString(), timesamp = ts, }; //得到新的数字 var room = http.getHtml(m.Groups[2].ToString()); r.activitycount = Spider.Between(room, "<div id=\"activityCount\">", "<"); r.livecount = Spider.Between(room, "id=\"live-count\">", "<"); r.livecount = r.livecount.Replace(",", ""); totalcount += int.Parse(r.livecount); rs.Add(r); } var tran = _db.BeginTransaction(); try { foreach (var r in rs) { var c = Cmd( $"INSERT into rec " + $"(roomid,title,nick,timesamp,livecount,activityCount,totalcount)" + $"values" + $"('{r.roomid}','{r.title.Replace("'", "")}','{r.nick}','{r.timesamp}','{r.livecount}','{r.activitycount}','{totalcount}')", tran ); } tran.Commit(); } catch (Exception ex) { logger.Error("写入数据库错误:" + ex); } sw.Stop(); label4.Text = sw.Elapsed.TotalSeconds + " S"; label6.Text = ms.Count.ToString(); logger.Info("count={0}, time={1}s done", ms.Count, sw.Elapsed.TotalSeconds); } }