1. 程式人生 > >Java獲取web資料(Java獲取股票資料)

Java獲取web資料(Java獲取股票資料)

使用Java8新特性獲取股票資料

2.獲取資料,就是解析這個data,程式碼如下

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.stream.Stream;

public class StokeData {

	private static String time ="2018-09-2809:00:00";
	private static int num = 1;
	private static boolean flag = false;

	private static BufferedWriter bw = null; 
	    public static void main(String[] args) throws InterruptedException, IOException {
	    	
	    	String url = "http://data.eastmoney.com/zjlx/detail.html";
	    	get(url);
    		for(num = 1;num <= 74 && flag; num++) {
    			get(url);
    		}
    		if(bw != null) {
    			bw.close();
    		}
	    	flag = false;
	    }



		private static void stream(Stream<String> lines) throws IOException, ParseException {
			String line = lines.filter(l ->  l.contains("dataurl:") )
			.map(l -> l.substring(l.lastIndexOf("token"), l.lastIndexOf("{")))
			//ChangePercent 漲跌排序
			//BalFlowMain 今日主力淨流入-> 淨額 排序
			
//			   <li data="C._AB">全部股票</li>
//             <li data="C._A">滬深A股</li>
//             <li data="C.2">滬市A股</li>
//             <li data="C._SZAME">深市A股</li>
//             <li data="C.80">創業板</li>
//             <li data="C.13">中小板</li>
//             <li data="C.3">滬市B股</li>
//             <li data="C.7">深市B股</li>
			
			
//			<li data="DCFFITA">今日排行<i></i></li>
//            <li data="DCFFITA3">3日排行<i></i></li>
//            <li data="DCFFITA5">5日排行<i></i></li>
//            <li data="DCFFITA10">10日排行<i></i></li>
			.map(l -> "http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=ct&st=(ChangePercent)&sr=-1&p=1&ps=50&js=var%20CHKjvtxf={pages:(pc),date:%222014-10-22%22,data:[(x)]}&"+l+"&cmd=C._AB&sty=DCFFITA")
			.map(l -> getData(l))
			.map(l -> l.substring(l.indexOf("[")+1,l.lastIndexOf("]")))
			.map(l -> l.replaceAll(" ", "").replaceAll("\t", ""))
			.flatMap(l -> Stream.of(l.split("\",\"")))
			. findFirst()
			.get().split(",")[15];
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
			Date date = sdf.parse(line);
			sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
			time = sdf.format(date);
			File f = new File("F:\\stoke_data\\"+time+".txt");
			flag = false;
			if(!f.exists()) {
				bw = new BufferedWriter(new FileWriter(f));
				flag = true;
			}
		}
		
		

		private static void write(Stream<String> lines) throws IOException {
			lines.filter(l ->  l.contains("dataurl:") )
			.map(l -> l.substring(l.lastIndexOf("token"), l.lastIndexOf("{")))
			.map(l -> "http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=ct&st=(ChangePercent)&sr=-1&p="+num+"&ps=50&js=var%20CHKjvtxf={pages:(pc),date:%222014-10-22%22,data:[(x)]}&"+l+"&cmd=C._AB&sty=DCFFITA")
			.map(l -> getData(l))
			.map(l -> l.substring(l.indexOf("[")+1,l.lastIndexOf("]")))
			.map(l -> l.replaceAll(" ", "").replaceAll("\t", ""))
			.flatMap(l -> Stream.of(l.split("\",\"")))
			.map(l -> l.replaceAll(" ", "").replaceAll("\"", ""))
			.forEach(l ->{
				
				try {
					bw.write(l);
					bw.newLine();
				} catch (IOException e) {
					System.out.println(l);
				}
			}
			);
			bw.flush();
		}


	   

	    /**
	     * 介面呼叫 GET
	     */
	    public static void get(String urls) {
	        try {
	            URL url = new URL(urls); // 把字串轉換為URL請求地址
	            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 開啟連線
	            if (System.currentTimeMillis()%2==0) {
					connection.setRequestProperty("Content-Type", "application/x-javascript");
					connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4094.1 Safari/537.36");
				}
				connection.connect();// 連線會話
	            // 獲取輸入流
	            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            Stream<String> lines = br.lines();
	           
	            if(!flag) {
		           stream(lines);
	            }else {
	            	write(lines);
	            }
	            
	            br.close();// 關閉流
	            connection.disconnect();// 斷開連線
//	            System.out.println(sb.toString());
	        } catch (Exception e) {
	            System.out.println("請求失敗!");
	            e.printStackTrace();
	        }
	    }
	    

	    /**
	     * 介面呼叫 GET
	     */
	    public static String getData(String urls) {
	        try {
	            URL url = new URL(urls); // 把字串轉換為URL請求地址
	            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 開啟連線
	            if (System.currentTimeMillis()%2==0) {
					connection.setRequestProperty("Content-Type", "application/x-javascript");
					connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4094.1 Safari/537.36");
				}
				connection.connect();// 連線會話
	            // 獲取輸入流
	            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	           String line = br.readLine();
	            br.close();// 關閉流
	            connection.disconnect();// 斷開連線
//	            System.out.println(sb.toString());
	            return line;
	        } catch (Exception e) {
	            System.out.println("請求失敗!");
	        }
	        return null;
	    }
}

3.程式碼分析

訪問web頁面

找到對應的token

拼接處資料的URL地址

檢視資料的日期

如果是新的資料,就寫到磁碟

4.如果對Java的語言不熟悉