public void GetAllFundTest() { WebService target = new WebService(); // TODO: 初始化为适当的值 //Assert.Inconclusive("验证此测试方法的正确性。"); string HtmlText = target.GetWebContent("http://fund.eastmoney.com/js/fundcode_search.js?v=20130718.js", Encoding.UTF8); 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>(); 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); } bool expected = true; bool actual = Fundlist.Count > 0; Assert.AreEqual(expected, actual); }
public void getNetValueTest() { WebService target = new WebService(); // TODO: 初始化为适当的值 string HtmlText = target.GetWebContent("http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code=000547&page=1&per=10000%22", Encoding.Default); //获取总记录数 int head = HtmlText.IndexOf("records:") + 8; int foot = HtmlText.IndexOf(",", head); int count = Convert.ToInt32(HtmlText.Substring(head, foot - head)); //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; 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); 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 (HtmlText.Length < 60) flag = 10000; } int expected = FundList2.Count; int actual; int value = 0; foreach (NetValueOfFund nvof in FundList2) { if (nvof.NetValue != null) value++; else value--; } actual = value; Assert.AreEqual(expected, actual); }