/
WebService.cs
363 lines (339 loc) · 19.4 KB
/
WebService.cs
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.NetworkInformation;
using System.IO;
using System.Windows.Forms;
namespace FundHelper
{
public class WebService
{
//获取整个网页
public String GetWebContent(String Url, Encoding encode)
{
try
{
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
Byte[] pageData = MyWebClient.DownloadData(Url); //从指定网站下载数据
return encode.GetString(pageData);
}
catch (WebException webEx)
{
Console.WriteLine(webEx.Message.ToString());
return null;
}
}
public List<Fund> GetAllFund()
{
String HtmlText;
try
{
HtmlText = GetWebContent("http://fund.eastmoney.com/js/fundcode_search.js?v=20130718.js", Encoding.UTF8);
}
catch (Exception)
{
return null;
}
HtmlText = HtmlText.Replace(",", "");
HtmlText = HtmlText.Replace("][", "");
HtmlText = HtmlText.Replace("\"\"", "\"");
//对处理过后的数据进行匹配
Regex regex = new Regex("(?<=\").+?(?=\")", RegexOptions.None);
MatchCollection MC = regex.Matches(HtmlText);
List<String> AllFund = new List<String>();
//将匹配后的数据存在一个字符串数组中
foreach (Match ma in MC)
{
AllFund.Add(ma.Value);
}
//按四个一组读,并将读后的数据存入基金类列表中
List<Fund> Fundlist = new List<Fund>();
try
{
for (int n = 0; n < AllFund.Count; n = n + 4)
{
Fund found = new Fund();
found.Code = AllFund[n];
found.Abbr = AllFund[n + 1];
found.Name = AllFund[n + 2];
found.Type = AllFund[n + 3];
Fundlist.Add(found);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
HtmlText = GetWebContent("http://fund.eastmoney.com/fund.html", Encoding.GetEncoding("gb2312"));
int head = 0;
head = HtmlText.IndexOf("type=\"checkbox\" id=", head);
int foot = 0;
String code;
Double netValueToday, totalNetValueToday, netValueInsToday, netValueInsRateToday;
while (head > 0)
{
head = HtmlText.IndexOf("</td><td>", head) + 1;
head = HtmlText.IndexOf("</td><td>", head) + 9;
foot = head + 6;
code = HtmlText.Substring(head, foot - head);
head = HtmlText.IndexOf("<td class=\"TD2\">", head);
head += 16;
foot = HtmlText.IndexOf("<", head);
netValueToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head));
head = HtmlText.IndexOf("<td class=\"TD2\">", head);
head += 16;
foot = HtmlText.IndexOf("<", head);
totalNetValueToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head));
head = HtmlText.IndexOf("<td class=", head) + 1;
head = HtmlText.IndexOf("<td class=", head) + 1;
head = HtmlText.IndexOf("<td class=", head) + 1;
head = HtmlText.IndexOf("'>", head);
head += 2;
foot = HtmlText.IndexOf("<", head);
netValueInsToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head));
foot += 10;
head = HtmlText.IndexOf(">", foot);
head += 1;
foot = HtmlText.IndexOf("<", head);
netValueInsRateToday = Convert.ToDouble(HtmlText.Substring(head, foot - head) == "---" ? "-1" : HtmlText.Substring(head, foot - head - 1));
foreach (Fund f in Fundlist)
{
if (f.Code == code)
{
f.NetValueToday = netValueToday;
f.TotalNetValueToday = totalNetValueToday;
f.NetValueInsToday = netValueInsToday;
f.NetValueInsRateToday = netValueInsRateToday;
}
}
head = HtmlText.IndexOf("type=\"checkbox\" id=", foot);
//MessageBox.Show(HtmlText.Substring(head, 500));
}
return Fundlist;
}
public List<NetValueOfFund> getNetValue(String code,ProgressBar progressBar)
{
String HtmlText;
try
{
HtmlText = GetWebContent("http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code=" + code + "&page=1&per=10000", Encoding.Default);
}
catch (Exception)
{
return null;
}
//获取总记录数
int head = HtmlText.IndexOf("records:") + 8;
int foot = HtmlText.IndexOf(",", head);
int count = Convert.ToInt32(HtmlText.Substring(head, foot-head));
progressBar.Maximum = count;
progressBar.Value = 0;
//return count;
int flag = 1;
String h2 = String.Empty;
String date = String.Empty;
String netValue = String.Empty;
String accumulativeNetValue = String.Empty;
String dailyGrowthRate = String.Empty;
String stateOfPurse = String.Empty;
String earningRecently = String.Empty;
String stateOfRedemption = String.Empty;
String distribution = String.Empty;
String earningPer10000 = String.Empty;
String earningPer7 = String.Empty;
int type;
String s = "<th>";
String e = "</th>";
String s1 = "<td>";
String e1 = "</td>";
String s2 = "<td class='tor bold'>";
String s3 = "<td class='tor bold red'>";
String s4 = "<td class='red unbold'>";
List<NetValueOfFund> FundList2 = new List<NetValueOfFund>();
//NetValueOfFund fund;
if (HtmlText.Length < 320)
flag = 10000;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</th>") + 4, HtmlText.Length - HtmlText.IndexOf("</th>") - 4);
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</th>") + 4, HtmlText.Length - HtmlText.IndexOf("</th>") - 4);
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</th>") + 4, HtmlText.Length - HtmlText.IndexOf("</th>") - 4);
Regex rg = new Regex("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline);
if (rg.Match(HtmlText).Value == "最近运作期年化收益率")
type = 1;
else if (rg.Match(HtmlText).Value == "日增长率")
type = 2;
else
type = 3;
if (type == 1)
{
while (flag != 10000)
{
if (HtmlText[HtmlText.IndexOf("<td") + 15] == 's')
{
date = HtmlText.Substring(HtmlText.IndexOf("<td") + 4, HtmlText.IndexOf("<span") - HtmlText.IndexOf("<td") - 4);
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
}
else
{
date = HtmlText.Substring(HtmlText.IndexOf("<td") + 4, HtmlText.IndexOf("</td>") - HtmlText.IndexOf("<td") - 4);
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
}
Regex rg2 = new Regex("(?<=(" + s2 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
earningPer10000 = rg2.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg3 = new Regex("(?<=(" + s2 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
earningPer7 = rg3.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg4 = new Regex("(?<=(" + s2 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
earningRecently = rg4.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg5 = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
stateOfPurse = rg5.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg6 = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
stateOfRedemption = rg6.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg7 = new Regex("(?<=(" + s4 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
distribution = rg7.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
FundList2.Add(new NetValueOfFund(date, netValue, earningPer10000, earningPer7, earningRecently, accumulativeNetValue, dailyGrowthRate, stateOfPurse, stateOfRedemption, distribution));
flag++;
if (progressBar.Value < progressBar.Maximum)
{
Application.DoEvents();
progressBar.Value++;
}
if (HtmlText.Length < 60)
flag = 10000;
}
//h2 = " 净值日期 每万份收益(元) 7日年化收益率(%) 最近运作期年化收益率 申购状态 赎回状态 分红送配" + "\r\n";
}
else if (type == 3)
{
while (flag != 10000)
{
if (HtmlText[HtmlText.IndexOf("<td") + 15] == 's')
{
date = HtmlText.Substring(HtmlText.IndexOf("<td") + 4, HtmlText.IndexOf("<span") - HtmlText.IndexOf("<td") - 4);
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
}
else
{
date = HtmlText.Substring(HtmlText.IndexOf("<td") + 4, HtmlText.IndexOf("</td>") - HtmlText.IndexOf("<td") - 4);
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
}
Regex rg2 = new Regex("(?<=(" + s2 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
earningPer10000 = rg2.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg3 = new Regex("(?<=(" + s2 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
earningPer7 = rg3.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg5 = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
stateOfPurse = rg5.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg6 = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
stateOfRedemption = rg6.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg7 = new Regex("(?<=(" + s4 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
distribution = rg7.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
FundList2.Add(new NetValueOfFund(date, netValue, earningPer10000, earningPer7, earningRecently, accumulativeNetValue, dailyGrowthRate, stateOfPurse, stateOfRedemption, distribution));
flag++;
if (progressBar.Value < progressBar.Maximum)
{
Application.DoEvents();
progressBar.Value++;
}
if (HtmlText.Length < 60)
flag = 10000;
}
//h2 = "净值日期 每万份收益(元) 7日年化收益率(%) 申购状态 赎回状态 分红送配" + "\r\n";
}
else
{
while (flag != 10000)
{
if (HtmlText[HtmlText.IndexOf("<td") + 15] == 's')
{
date = HtmlText.Substring(HtmlText.IndexOf("<td") + 4, HtmlText.IndexOf("<span") - HtmlText.IndexOf("<td") - 4);
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
}
else
{
date = HtmlText.Substring(HtmlText.IndexOf("<td") + 4, HtmlText.IndexOf("</td>") - HtmlText.IndexOf("<td") - 4);
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
}
Regex rg2 = new Regex("(?<=(" + s2 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
netValue = rg2.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg3 = new Regex("(?<=(" + s2 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
accumulativeNetValue = rg3.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg4 = new Regex("(?<=(" + s3 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
dailyGrowthRate = rg4.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg5 = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
stateOfPurse = rg5.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg6 = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
stateOfRedemption = rg6.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
Regex rg7 = new Regex("(?<=(" + s4 + "))[.\\s\\S]*?(?=(" + e1 + "))", RegexOptions.Multiline | RegexOptions.Singleline);
distribution = rg7.Match(HtmlText).Value;
HtmlText = HtmlText.Substring(HtmlText.IndexOf("</td>") + 4, HtmlText.Length - HtmlText.IndexOf("</td>") - 4);
FundList2.Add(new NetValueOfFund(date, netValue, earningPer10000, earningPer7, earningRecently, accumulativeNetValue, dailyGrowthRate, stateOfPurse, stateOfRedemption, distribution));
flag++;
if (progressBar.Value < progressBar.Maximum)
{
Application.DoEvents();
progressBar.Value++;
}
if (HtmlText.Length < 60)
flag = 10000;
}
//h2 = "净值日期 单位净值(元) 累计净值(元) 日增长率 申购状态 赎回状态 分红送配" + "\r\n";
}
return FundList2;
#region 基金数据写入文件的方法
//new NetValueOfFund(date, netValue, accumulativeNetValue, dailyGrowthRate, stateOfPurse, stateOfRedemption);
/* 导出
String result2 = String.Empty;
String FILE_NAME2 = "D:\\funds\\" + fundCode + ".txt";
StreamWriter sr;
sr = File.CreateText(FILE_NAME2);
if (type == 3)
{
for (int m = 0; m < FundList2.Count; m++)
{
h2 += FundList2[m].date + '\t' + FundList2[m].earningPer10000 + '\t' + FundList2[m].earningPer7
+ '\t' + FundList2[m].stateOfPurse + '\t' + FundList2[m].stateOfRedemption + FundList2[m].distribution + "\r\n";
}
}
else if (type == 1)
{
for (int m = 0; m < FundList2.Count; m++)
{
h2 += FundList2[m].date + '\t' + FundList2[m].earningPer10000 + '\t' + FundList2[m].earningPer7 + FundList2[m].earningRecently + '\t'
+ '\t' + FundList2[m].stateOfPurse + '\t' + FundList2[m].stateOfRedemption + FundList2[m].distribution + "\r\n";
}
}
else
{
for (int m = 0; m < FundList2.Count; m++)
{
h2 += FundList2[m].date + '\t' + FundList2[m].netValue + '\t' + FundList2[m].accumulativeNetValue
+ '\t' + FundList2[m].dailyGrowthRate + '\t' + FundList2[m].stateOfPurse + '\t'
+ FundList2[m].stateOfRedemption + FundList2[m].distribution + "\r\n";
}
}
sr.Write(h2);
sr.Close();
*/
#endregion
}
}
}