コード例 #1
0
ファイル: WebService.cs プロジェクト: JNU-Agile/JNU-Agile
        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);
        }