整个程序是借用了新浪的对外股票接口http://hq.sinajs.cn/list=sh603696,如果把这个地址放到浏览器地址栏里,你将看到:
var hq_str_sh603696="安记食品,8.110,8.120,8.020,8.160,7.960,8.020,8.030,3471565,27941477.000,400,8.020,58800,8.010,25500,8.000,2688,7.990,29600,7.980,23000,8.030,19700,8.040,16600,8.050,10800,8.060,5200,8.070,2020-02-21,15:00:00,00,";
下面程序要做的就是把返回的这段文本解析出来。
程序:
package single; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; /** * 用于获取收盘后股票基本信息 * 如果是当天即本日信息 * 如股票不是当天则是上一个交易日 * 主要代码转载自:https://blog.csdn.net/weixin_34185320/article/details/94747010 * */ public class StockInfoCollecter { public static String getHtmlConentByUrl(String ssourl) { try { URL url = new URL(ssourl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setInstanceFollowRedirects(false); con.setUseCaches(false); con.setAllowUserInteraction(false); con.connect(); StringBuffer sb = new StringBuffer(); String line = ""; BufferedReader URLinput = new BufferedReader(new InputStreamReader( con.getInputStream())); while ((line = URLinput.readLine()) != null) { sb.append(line); } con.disconnect(); return sb.toString().toLowerCase(); } catch (Exception e) { return null; } } public static void main(String[] args) { String[] ind = {"股票名称","今日开盘价","昨日收盘价","当前价格","今日最高价","今日最低价","买一报价","卖一报价","成交股数", "成交金额","买一股数","买一报价","买二股数","买二报价","买三股数","买三报价","买四股数","买四报价","买五股数","买五报价", "卖一股数","卖一报价","卖二股数","卖二报价","卖三股数","卖三报价","卖四股数","卖四报价","卖五股数","卖五报价","交易日日期","收盘时间"}; String ss = getHtmlConentByUrl("http://hq.sinajs.cn/list=sh"+"603696");//你要访问的股票 String[] fcs = ss.split("""); //按"号分割字符 String scs = fcs[1]; String[] tcs = scs.split(","); //按,号分割字符 for(int i=0;i<tcs.length-1;i++) System.out.println(ind[i] + ": " +tcs[i]); } }
输出:
股票名称: 安记食品 今日开盘价: 8.110 昨日收盘价: 8.120 当前价格: 8.020 今日最高价: 8.160 今日最低价: 7.960 买一报价: 8.020 卖一报价: 8.030 成交股数: 3471565 成交金额: 27941477.000 买一股数: 400 买一报价: 8.020 买二股数: 58800 买二报价: 8.010 买三股数: 25500 买三报价: 8.000 买四股数: 2688 买四报价: 7.990 买五股数: 29600 买五报价: 7.980 卖一股数: 23000 卖一报价: 8.030 卖二股数: 19700 卖二报价: 8.040 卖三股数: 16600 卖三报价: 8.050 卖四股数: 10800 卖四报价: 8.060 卖五股数: 5200 卖五报价: 8.070 交易日日期: 2020-02-21 收盘时间: 15:00:00
转载地址:
https://www.cnblogs.com/tiandsp/p/7467848.html
https://blog.csdn.net/weixin_34185320/article/details/94747010
在此感谢作者的辛苦劳动付出。
--2020年2月24日--