【Java】得到當前股票資訊

Dsp Tian發表於2017-09-02
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class MainProcess {
     
    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=sh600066");//你要訪問的頁面  
        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]);     
    }
}

 

相關文章