My Computer Programs

(Java) get web page, KOSPI200 시세 얻기

steloflute 2012. 12. 11. 23:30

아래는 koscom 에서 kospi200 시세를 얻어오는 예제이다.

 

import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class showindex {
	public static String readURL(String address) throws IOException {		
		java.net.URL url = new java.net.URL(address);		
		InputStream stream = url.openStream();
		java.io.BufferedReader buf = new java.io.BufferedReader(new java.io.InputStreamReader(stream));
		
		String r = "";
		while(true) {
			String s = buf.readLine();
			if (s == null) break;
			r += s + "\n";
		}
		
		buf.close();
		stream.close();
		return r;
	}
	
	public static String getQuote() throws IOException {
		String text = readURL("http://kosdb.koscom.co.kr/main/jisuticker.html");
		Pattern p = Pattern.compile("KOSPI200.*</font>");
		Matcher m = p.matcher(text);
		if (m.find()) {
			return m.group();
		}
		return "";
	}

	public static void main(String[] args) throws IOException, InterruptedException {
		while(true) {
			Calendar now = Calendar.getInstance();
			System.out.println(new Date());
			System.out.println(getQuote());
			Thread.sleep(60 * 1000);
			while (true) {
				int hour = now.get(Calendar.HOUR);
				if (hour >= 9 && hour <= 14) break;
				Thread.sleep(60 * 1000);
			}
		}
	}
}

 

 

 

 

showindex.zip

 

See also

http://steloflute.tistory.com/search/web%20page

'My Computer Programs' 카테고리의 다른 글

(C++) pfcalc: Postfix Calculator  (0) 2013.01.28
DrClojure  (0) 2012.12.23
(Clojure) get web page, KOSPI200 시세 얻기  (0) 2012.12.11
cppcalc  (0) 2012.12.09
Tg - Esolang  (0) 2012.11.11