抓取投票数据,某大神写的 备注参考

1:   

   2:  static void Main(string[] args)
   3:  {
   4:      timer(null, null);
   5:   
   6:      System.Timers.Timer aTimer = new System.Timers.Timer();
   7:      aTimer.Elapsed += new ElapsedEventHandler(timer);  
   8:      aTimer.Interval = 30 * 1000;
   9:      aTimer.AutoReset = true;
  10:      aTimer.Start();
  11:      
  12:      Console.Read();
  13:  }
  14:   
  15:  private static void timer(object source, System.Timers.ElapsedEventArgs e)
  16:  {
  17:      string pageHtml = GetWebPageContent();
  18:   
  19:      HtmlDocument doc = new HtmlDocument();
  20:      doc.OptionFixNestedTags = true;
  21:      doc.LoadHtml(pageHtml);
  22:   
  23:      HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//a[@class='photo']");
  24:   
  25:      string datetime = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss");
  26:   
  27:      for (int i = 0; i < 40; i++)
  28:      {
  29:          HtmlNode node = nodes[i];
  30:   
  31:          string title = node.Attributes["title"].Value;
  32:   
  33:          string nodeText = node.InnerText.Trim();
  34:          nodeText = nodeText.Substring(2, nodeText.Length - 3);
  35:   
  36:          int piaoshu = Convert.ToInt32(nodeText);
  37:   
  38:          Console.WriteLine(piaoshu + "\t" + title);
  39:   
  40:          WriteToFile(title, piaoshu, datetime);
  41:   
  42:      }
  43:   
  44:      Console.WriteLine(datetime + " waiting...");
  45:  }
  46:   
  47:  private static void WriteToFile(string title, int piaoshu, string datetime)
  48:  {
  49:      string filePath = Path.Combine(Environment.CurrentDirectory, @"result\" + title + ".txt");
  50:   
  51:      using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
  52:      {
  53:          sw.WriteLine(datetime + "\t" + piaoshu);
  54:      }
  55:  }
  56:   
  57:  private static string GetWebPageContent()
  58:  {
  59:      WebClient MyWebClient = new WebClient();
  60:   
  61:      MyWebClient.Credentials = CredentialCache.DefaultCredentials;
  62:   
  63:      Byte[] pageData = MyWebClient.DownloadData("https://code.csdn.net/2013OSSurvey/gitop/codevote/vote_num");
  64:   
  65:      string pageHtml = Encoding.UTF8.GetString(pageData);
  66:   
  67:      return pageHtml;
  68:  }



转载于:https://www.cnblogs.com/kinggod/articles/3680357.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值