1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
| using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
namespace Ticket
{
class Program
{
public static void StationNameXml()
{
string nowpath = Environment.CurrentDirectory;
if (Directory.Exists(nowpath + "/Data") == false)
Directory.CreateDirectory(nowpath + "/Data");//创建新路径
if (File.Exists(nowpath + "/Data/StationName.xml") == true)
return;
HttpWebRequest request;
HttpWebResponse response;
string url = @"https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9055";
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Credentials = CredentialCache.DefaultCredentials;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36";
string str = string.Empty;
try
{
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
str = sr.ReadToEnd();
sr.Close();
response.Close();//关闭response响应流
}
else
throw new Exception();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
string pattern = @"@\S{2,5}?[|](?<Name>\S{1,8}?)[|](?<Code>\S{3,5}?)[|]";
Regex r = new Regex(pattern, RegexOptions.ExplicitCapture);
XmlDocument xmldoc = new XmlDocument();
XmlDeclaration xmldecl;
xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);//加入XML的声明段落
xmldoc.AppendChild(xmldecl);
XmlElement root = xmldoc.CreateElement("", "Dict", "");//加入一个根元素
xmldoc.AppendChild(root);
MatchCollection mc = r.Matches(str);
foreach (Match m in mc)
{
GroupCollection group = m.Groups;
XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, "Pair", null);
XmlNode temp = xmldoc.CreateNode(XmlNodeType.Element, "Station", null);
temp.InnerText = group["Name"].Value;
node.AppendChild(temp);
XmlNode temp2 = xmldoc.CreateNode(XmlNodeType.Element, "Code", null);
temp2.InnerText = group["Code"].Value;
node.AppendChild(temp2);
root.AppendChild(node);
}
xmldoc.Save(nowpath + "/Data/" + "StationName.xml");
}
private static string StationCode(string StationName)
{
XmlDocument doc = new XmlDocument();
doc.Load(Environment.CurrentDirectory + "/Data/StationName.xml");
XmlNodeList data = doc.DocumentElement.ChildNodes;
foreach (XmlNode node in data)
{
XmlElement xe = (XmlElement)node;
XmlNodeList xnl0 = xe.ChildNodes;
string s = xnl0.Item(0).InnerText;
if (s == StationName)
{
return xnl0.Item(1).InnerText;
}
}
return string.Empty;
}
private static Ticket[] SpiderTicket(string date, string from, string to, string id)
{
HttpWebRequest request;
HttpWebResponse response;
string url = "https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={0}&leftTicketDTO.from_station={1}&leftTicketDTO.to_station={2}&purpose_codes={3}";
url = string.Format(url, date, from, to, id);
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Credentials = CredentialCache.DefaultCredentials;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36";
string str = string.Empty;
try
{
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
str = sr.ReadToEnd();
sr.Close();
response.Close();//关闭response响应流
}
else
throw new Exception();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Dictionary<string, string> map=new Dictionary<string, string>();//保存返回的JSON串中的车站代码与车站名之间的映
ArrayList AllTicket = new ArrayList();//所有的车票信息
JObject jo = (JObject)JsonConvert.DeserializeObject(str);
JToken record = jo["data"]["map"];
foreach (JProperty item in record)
{
map.Add(item.Name.ToString(), item.Value.ToString());
}
record = jo["data"]["result"];
foreach (string s in record)
{
string[] result = s.Split('|');
Ticket t = new Ticket();
t.TrainId = result[3];
t.FromStation = map[result[6]];
t.ToStation = map[result[7]];
t.StartTime = result[8];
t.ArriveTime = result[9];
t.LastedTime = result[10];
t.TicketDate = result[13];
t.DwNum = result[33] != "" ? result[33] : t.DwNum;
t.TzNum = result[32] != "" ? result[32] : t.TzNum;
t.YdNum = result[31] != "" ? result[31] : t.YdNum;
t.EdNum = result[30] != "" ? result[30] : t.EdNum;
t.YzNum = result[29] != "" ? result[29] : t.YzNum;
t.YwNum = result[28] != "" ? result[28] : t.YwNum;
t.WzNum = result[26] != "" ? result[26] : t.WzNum;
t.RzNum = result[24] != "" ? result[24] : t.RzNum;
t.RwNum = result[23] != "" ? result[23] : t.RwNum;
t.GrwNum = result[21] != "" ? result[21] : t.GrwNum;
AllTicket.Add(t);
}
Ticket[] All = new Ticket[AllTicket.Count];
int flag = 0;
foreach (object t in AllTicket)
{
All[flag++] = t as Ticket;
}
return All;
}
static void Main(string[] args)
{
StationNameXml();
string from = "上海";
string to = "北京";
Ticket[] All=new Ticket[50];
All=SpiderTicket(DateTime.Now.ToString("yyyy-MM-dd"), StationCode(from), StationCode(to), "ADULT");
for(int i=0;i<All.Length;i++)
{
Dictionary<string, string> map = new Dictionary<string, string>();
map = All[i].getMap();
foreach (object j in map.Keys)
Console.Write(j.ToString()+":"+map[j.ToString()].ToString()+" ");
Console.WriteLine();
}
Console.Read();
}
}
}
|