using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.ComponentModel; using System.Threading; using System.Text.RegularExpressions; using System.Diagnostics; namespace HuoChePiao { class Program { private static HashSet<string> results; private static WebClient wc; private static List<Site> sites; private static int index; private static bool isFirstRound; static void Main(string[] args) { sites = new List<Site>(); sites.Add(new Site() { Name = "火车票网", Url = "http://www.huochepiao.com/City/SearchCheCi.asp?leixing=%D7%AA%C8%C3&zhuti=t189&psearch=%C7%F3%B9%BA%2F%D7%AA%C8%C3%BC%EC%CB%F7", RegexPattern = @"· <A href=(.*?) target=_blank>(.*?)</a>", Encoding = Encoding.Default }); sites.Add(new Site() { Name = "百姓网", Url = "http://beijing.baixing.com/huochepiao/?%E8%BD%A6%E6%AC%A1=T189", RegexPattern = @""" ><a href=""/(.*?)"">(.*?)</a></td>", Encoding = Encoding.UTF8, Domain = "http://beijing.baixing.com/", Keys = new string[] { "卧" } }); sites.Add(new Site() { Name = "百姓网", Url = "http://beijing.baixing.com/huochepiao/?%E8%BD%A6%E6%AC%A1=T5", RegexPattern = @""" ><a href=""/(.*?)"">(.*?)</a></td>", Encoding = Encoding.UTF8, Domain = "http://beijing.baixing.com/", Keys = new string[] { "卧" } }); sites.Add(new Site() { Name = "酷讯网", Url = "http://huoche.kuxun.cn/zhuanrang-checi-beijing-T189.html?type=1", RegexPattern = @"<span class=""col_11 left""><a target=""_blank"" href=""(.*)"">(.*)</a>", Encoding = Encoding.UTF8 }); sites.Add(new Site() { Name = "酷讯网", Url = "http://huoche.kuxun.cn/zhuanrang-checi-beijing-T5.html?type=1", RegexPattern = @"<span class=""col_11 left""><a target=""_blank"" href=""(.*)"">(.*)</a>", Encoding = Encoding.UTF8 }); sites.Add(new Site() { Name = "酷讯网", Url = "http://huoche.kuxun.cn/zhuanrang-checi-beijing-k185.html?type=1", RegexPattern = @"<span class=""col_11 left""><a target=""_blank"" href=""(.*)"">(.*)</a>", Encoding = Encoding.UTF8 }); sites.Add(new Site() { Name = "清华网", Url = "http://www.newsmth.net/bbsbfind.php?q=1&board=Ticket&title=T189&title3=%C7%F3&dt=1", RegexPattern = @"<a href=""(bbscon.php.*)"">(.*)</a>", Encoding = Encoding.Default, Domain = "http://www.newsmth.net/" }); sites.Add(new Site() { Name = "清华网", Url = "http://www.newsmth.net/bbsbfind.php?q=1&board=Ticket&title=K185&title3=%C7%F3&dt=1", RegexPattern = @"<a href=""(bbscon.php.*)"">(.*)</a>", Encoding = Encoding.Default, Domain = "http://www.newsmth.net/" }); results = new HashSet<string>(); isFirstRound = true; wc = new WebClient(); wc.Encoding = sites[index].Encoding; wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); wc.DownloadStringAsync(new Uri(sites[index].Url)); int order; while (int.TryParse(Console.ReadLine(), out order)) { OpenLink(results.ElementAt(order - 1)); Console.ForegroundColor = ConsoleColor.Green; } } static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { int i = 0; MatchCollection mc = Regex.Matches(e.Result, sites[index].RegexPattern); foreach (Match m in mc) { if (m.Success) { string result = sites[index].Domain + m.Groups[1].Value; string content = m.Groups[2].Value; if (!results.Contains(result)) { bool isContainKey = true; if (sites[index].Keys != null) { foreach (string key in sites[index].Keys) { if (!content.Contains(key)) { isContainKey = false; break; } } } if (!isContainKey) continue; results.Add(result); Console.ForegroundColor = isFirstRound ? ConsoleColor.Gray : ConsoleColor.White; Console.WriteLine("{0} <{1}> {2}. {3}", DateTime.Now.ToShortTimeString(), sites[index].Name, results.Count, m.Groups[2].Value); if (!isFirstRound) { OpenLink(result); } } } if (i++ > sites[index].Numbers - 2) break; } Thread.Sleep(1000); index = index + 1; if (index == sites.Count) { index = 0; if (isFirstRound) isFirstRound = false; } wc.Encoding = sites[index].Encoding; wc.DownloadStringAsync(new Uri(sites[index].Url)); } static void OpenLink(string url) { Process.Start(url, "_blank"); } } public class Site { //站点名称 public string Name { get; set; } //站点的网址 public string Url { get; set; } //正则表达式 public string RegexPattern { get; set; } //编码 public Encoding Encoding { get; set; } //网站名称 public string Domain { get; set; } //包含关键字 public string[] Keys { get; set; } //取前面多少条 public int Numbers { get; set; } public Site() { this.Numbers = 3; } } }
每年都用这个抢到票了,今天也不例外,分享给大家:
1. 里面的规则你可以自己添加,我已经写好了很多,你把网址里德车次改下就行了
2. 初始化会抓取最新的信息,但是不弹出网页,你可以在控制台输入id号,弹出对应id号的网址
3. 后面会自动循环捕获每个站点的新信息,并弹出网页
快就一个字,希望大家都能买到火车票,安心回家