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: }