개발_이야기

jsp 페이지 얻기 & 파싱하기

조규현15 2015. 11. 6. 08:49
반응형

java로 작업하면 쉽다...

왜냐하면 다 만들어져 있어서...


1. htmlloader로 페이지를 얻고

2. parsing을 한다.

3. data를 얻는다.


아래는 메인 클래스임

package game;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.AsyncTask;
public class getData {
	public static String Data;
	
	public static void callRequestData()
	{
		Data = "";
		new HTMLTask().execute(null, null, null);
	} 
	private static class HTMLTask extends AsyncTask{
		@Override
		protected Void doInBackground(Void... params) {
			try{
				Data = getHTML("url"
			            ,"갑천");
			} catch(Exception e){
				e.printStackTrace();
			}
			return null;
		}
	}
	 private static String getHTML(String urlToRead, String TagName) {
         URL url; // The URL to read
         HttpURLConnection conn; // The actual connection to the web page
         BufferedReader rd; // Used to read results from the web page
         String line; // An individual line of the web page HTML
         String result = ""; // A long string containing all the HTML
         
         int Crawl_start= -1;
         try {
            url = new URL(urlToRead);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "EUC_KR"));
            while ((line = rd.readLine()) != null) {
            	if(Crawl_start == -1 && line.contains(TagName))
                  Crawl_start = 0;
               else if(Crawl_start == 0)
               {
                  if(line.contains("수온"))
                  {
                     result = line;
                     Crawl_start = 1;
                  }
                  else
                     Crawl_start = -1;
               }
            }
            rd.close();
         } catch (Exception e) {
            e.printStackTrace();
         }
         
         return result.substring(14, 18);
      }
   
}

이걸로 '풍덩'도 만들었다.

반응형