当咱们须要经过网络来自动获取股指或股票的深度行情时,通常有如下两种方法能够得到。
目前除了使用Python进行爬虫获取(须要解析html得到)外还能够经过新浪提供的JS行情服务器得到,本文采用的是后者(仍是很是方便的)。本文采用新浪JS获取的方式,主要有两种方法:
一、查询股指或股票若采用这种格式(s_yyXXXXXX)通常返回的含有如下字段:
指数名称,当前点数,涨跌幅,涨跌率,成交量(手),成交额(万元);
二、查询股指或股票采用这种格式(yyXXXXXX)通常返回的含有如下字段:
股票名称,今开盘,昨收盘,最新价,最高价,最低价,买一价,卖一价,成交量,成交额(万元),买一量,买一价,...,买五量,买五价,卖一量,卖一价,...,卖五量,买五价,日期,时间
因为第一种方法没有直接给出昨收盘须要计算获得。
Show me your code:
1 private static double GetThreeIndexBySina() 2 { 3 double precloseindex = 0; 4 try 5 { 6 //s_yyXXXXXX格式数据返回含义分别为:指数名称,当前点数,涨跌幅,涨跌率,成交量(手),成交额(万元); 7 //股票查询规则:sh601857,sz002230:中石油,科大讯飞(以sh开头表明沪市A股,以sz开头表明深市股票,后面是对应的股票代码) 8 //股指查询规则:s_sh000001,s_sz399001,s_sz399106,s_sh000300:上证指数,深证成指,深证综指,沪深300 9 string url = "http://hq.sinajs.cn/list=s_sh000300"; 10 HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); 11 HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse(); 12 Stream stream = webreponse.GetResponseStream(); 13 byte[] rsByte = new Byte[webreponse.ContentLength]; //save data in the stream 14 stream.Read(rsByte, 0, (int)webreponse.ContentLength); 15 string tmp = System.Text.Encoding.UTF8.GetString(rsByte, 0, rsByte.Length).ToString(); 16 string[] index = tmp.Split('"'); 17 string[] datas = index[1].Split(','); 18 double del = Convert.ToDouble(datas[2]); 19 precloseindex = Convert.ToDouble(datas[1]); 20 precloseindex = Math.Round(precloseindex - del, 2, MidpointRounding.AwayFromZero); 21 } 22 catch (Exception exp) 23 { 24 Console.WriteLine(exp.Message); 25 } 26 return precloseindex; 27 }
以上是为了使用第一种方法获取昨收盘(比较精简),若是怕麻烦能够直接使用第二种方法直接得到。。。html