抢火车票利器(今年试试)
001 |
using System; |
002 |
using System.Collections.Generic; |
003 |
using System.Linq; |
004 |
using System.Text; |
005 |
using System.Net; |
006 |
using System.ComponentModel; |
007 |
using System.Threading; |
008 |
using System.Text.RegularExpressions; |
009 |
using System.Diagnostics; |
010 |
011 |
namespace HuoChePiao |
012 |
{ |
013 |
class Program |
014 |
{ |
015 |
private static HashSet<string> results; |
016 |
private static WebClient wc; |
017 |
private static List<Site> sites; |
018 |
private static int index; |
019 |
private static bool isFirstRound; |
020 |
|
021 |
static void Main(string[] args) |
022 |
{ |
023 |
sites = new List<Site>(); |
024 |
|
025 |
sites.Add(new Site() |
026 |
{ |
027 |
Name = "火车票网", |
029 |
RegexPattern = @"· <A href=(.*?) target=_blank>(.*?)</a>", |
030 |
Encoding = Encoding.Default |
031 |
}); |
032 |
|
033 |
sites.Add(new Site() |
034 |
{ |
035 |
Name = "百姓网", |
037 |
RegexPattern = @""" ><a href=""/(.*?)"">(.*?)</a></td>", |
038 |
Encoding = Encoding.UTF8, |
039 |
Domain = "http://beijing.baixing.com/", |
040 |
Keys = new string[] { "卧" } |
041 |
}); |
042 |
043 |
sites.Add(new Site() |
044 |
{ |
045 |
Name = "百姓网", |
047 |
RegexPattern = @""" ><a href=""/(.*?)"">(.*?)</a></td>", |
048 |
Encoding = Encoding.UTF8, |
049 |
Domain = "http://beijing.baixing.com/", |
050 |
Keys = new string[] { "卧" } |
051 |
}); |
052 |
|
053 |
sites.Add(new Site() |
054 |
{ |
055 |
Name = "酷讯网", |
057 |
RegexPattern = @"<span class=""col_11 left""><a target=""_blank"" href=""(.*)"">(.*)</a>", |
058 |
Encoding = Encoding.UTF8 |
059 |
}); |
060 |
061 |
sites.Add(new Site() |
062 |
{ |
063 |
Name = "酷讯网", |
065 |
RegexPattern = @"<span class=""col_11 left""><a target=""_blank"" href=""(.*)"">(.*)</a>", |
066 |
Encoding = Encoding.UTF8 |
067 |
}); |
068 |
069 |
sites.Add(new Site() |
070 |
{ |
071 |
Name = "酷讯网", |
073 |
RegexPattern = @"<span class=""col_11 left""><a target=""_blank"" href=""(.*)"">(.*)</a>", |
074 |
Encoding = Encoding.UTF8 |
075 |
}); |
076 |
077 |
sites.Add(new Site() |
078 |
{ |
079 |
Name = "清华网", |
081 |
RegexPattern = @"<a href=""(bbscon.php.*)"">(.*)</a>", |
082 |
Encoding = Encoding.Default, |
083 |
Domain = "http://www.newsmth.net/" |
084 |
}); |
085 |
086 |
sites.Add(new Site() |
087 |
{ |
088 |
Name = "清华网", |
090 |
RegexPattern = @"<a href=""(bbscon.php.*)"">(.*)</a>", |
091 |
Encoding = Encoding.Default, |
092 |
Domain = "http://www.newsmth.net/" |
093 |
}); |
094 |
|
095 |
results = new HashSet<string>(); |
096 |
097 |
isFirstRound = true; |
098 |
099 |
wc = new WebClient(); |
100 |
wc.Encoding = sites[index].Encoding; |
101 |
102 |
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); |
103 |
wc.DownloadStringAsync(new Uri(sites[index].Url)); |
104 |
105 |
int order; |
106 |
while (int.TryParse(Console.ReadLine(), out order)) |
107 |
{ |
108 |
OpenLink(results.ElementAt(order - 1)); |
109 |
Console.ForegroundColor = ConsoleColor.Green; |
110 |
} |
111 |
} |
112 |
113 |
static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) |
114 |
{ |
115 |
int i = 0; |
116 |
|
117 |
MatchCollection mc = Regex.Matches(e.Result, sites[index].RegexPattern); |
118 |
foreach (Match m in mc) |
119 |
{ |
120 |
if (m.Success) |
121 |
{ |
122 |
string result = sites[index].Domain + m.Groups[1].Value; |
123 |
string content = m.Groups[2].Value; |
124 |
if (!results.Contains(result)) |
125 |
{ |
126 |
bool isContainKey = true; |
127 |
if (sites[index].Keys != null) |
128 |
{ |
129 |
foreach (string key in sites[index].Keys) |
130 |
{ |
131 |
if (!content.Contains(key)) |
132 |
{ |
133 |
isContainKey = false; |
134 |
break; |
135 |
} |
136 |
} |
137 |
} |
138 |
139 |
if (!isContainKey) |
140 |
continue; |
141 |
142 |
results.Add(result); |
143 |
144 |
Console.ForegroundColor = isFirstRound ? ConsoleColor.Gray : ConsoleColor.White; |
145 |
146 |
Console.WriteLine("{0} <{1}> {2}. {3}", DateTime.Now.ToShortTimeString(), sites[index].Name, results.Count, m.Groups[2].Value); |
147 |
148 |
if (!isFirstRound) |
149 |
{ |
150 |
OpenLink(result); |
151 |
} |
152 |
} |
153 |
} |
154 |
155 |
if (i++ > sites[index].Numbers - 2) |
156 |
break; |
157 |
} |
158 |
159 |
Thread.Sleep(1000); |
160 |
161 |
index = index + 1; |
162 |
if (index == sites.Count) |
163 |
{ |
164 |
index = 0; |
165 |
166 |
if (isFirstRound) |
167 |
isFirstRound = false; |
168 |
} |
169 |
170 |
wc.Encoding = sites[index].Encoding; |
171 |
wc.DownloadStringAsync(new Uri(sites[index].Url)); |
172 |
} |
173 |
174 |
static void OpenLink(string url) |
175 |
{ |
176 |
Process.Start(url, "_blank"); |
177 |
} |
178 |
} |
179 |
180 |
public class Site |
181 |
{ |
182 |
//站点名称 |
183 |
public string Name { get; set; } |
184 |
//站点的网址 |
185 |
public string Url { get; set; } |
186 |
//正则表达式 |
187 |
public string RegexPattern { get; set; } |
188 |
//编码 |
189 |
public Encoding Encoding { get; set; } |
190 |
//网站名称 |
191 |
public string Domain { get; set; } |
192 |
//包含关键字 |
193 |
public string[] Keys { get; set; } |
194 |
//取前面多少条 |
195 |
public int Numbers { get; set; } |
196 |
197 |
public Site() |
198 |
{ |
199 |
this.Numbers = 3; |
200 |
} |
201 |
} |
202 |
} |

每年都用这个抢到票了,今天也不例外,分享给大家:
1. 里面的规则你可以自己添加,我已经写好了很多,你把网址里德车次改下就行了
2. 初始化会抓取最新的信息,但是不弹出网页,你可以在控制台输入id号,弹出对应id号的网址
3. 后面会自动循环捕获每个站点的新信息,并弹出网页
快就一个字,希望大家都能买到火车票,安心回家

浙公网安备 33010602011771号