public IList <data.stock> GetStocks(data.stockcategory category) { IList <string> codes = getStockCodeList(category); IList <IList <string> > codeStr = new List <IList <string> >(); int num = 10; if (codes.Count > 10) { for (int i = 0; i < codes.Count; i = i + num) { codeStr.Add(codes.Skip(i).Take(num).ToList()); } } else { codeStr.Add(codes); } IList <data.stock> stockList = new List <data.stock>(); foreach (IList <string> sub in codeStr) { string url2 = "http://push3.gtimg.cn/q={0}&m=push&r={1}"; Uri uri = new Uri(string.Format(url2, string.Join(",", sub), new Random().ToString())); //pv_sz000718="51~苏宁环球~000718~4.65~4.63~4.65~181492~70761~110731~4.64~1372~4.63~3972~4.62~3000~4.61~5423~4.60~1117~4.65~3246~4.66~1875~4.67~2492~4.68~3955~4.69~2481~15:00:20/4.65/3400/S/1581372/21958|14:56:56/4.65/50/S/23250/21784|14:56:53/4.66/20/B/9320/21779|14:56:47/4.65/29/S/13509/21768|14:56:38/4.65/14/S/6510/21756|14:56:38/4.66/24/B/11184/21750~20140704150411~0.02~0.43~4.69~4.58~4.65/178092/82605364~181492~8419~1.23~103.55~~4.69~4.58~2.38~68.35~95.01~2.19~5.09~4.17~"; string content = getRequestContent(uri); string[] stockStrs = content.Split('\n'); foreach (string stockStr in stockStrs) { Regex reg = new Regex("\"[^\"]+\""); var matchs = reg.Matches(stockStr); foreach (Match match in matchs) { string[] vs = match.Value.Trim('\"').Split('~'); data.stock stock = new data.stock(); stock.code = (vs[0] == "51" ? "1" : "0") + vs[2]; //stock.cate_code = category.code; stock.name = vs[1]; stock.price = decimal.Parse(vs[3]); stock.yestclose = decimal.Parse(vs[4]); stock.symbol = ""; stockList.Add(stock); Console.WriteLine(stock.code + "-" + stock.name + "-" + stock.price); } } } return(stockList); }
private IList <string> getStockCodeList(data.stockcategory category) { string url = "http://stock.gtimg.cn/data/index.php?appn=rank&t=pt{0}/chr&p=1&o=0&l=100000&v=list_data"; Uri uri = new Uri(string.Format(url, category.code)); string content = getRequestContent(uri); Regex r = new Regex(@"s[zh]{1}\d{6}"); MatchCollection matchs = r.Matches(content); //content.Substring(content.IndexOf("data:'"), IList <string> codes = new List <string>(); foreach (Match match in matchs) { codes.Add(match.Value); } return(codes); }