jsoup lib를 이용하여 크롤링 연습하기

 

소스 다운 및 참고사이트 : https://jsoup.org/

 

public class CommonCrolling {
    private final static String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36";
   
    public static String getCurrentData(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return sdf.format(new Date());
    }
   
    public static void main(String[] args) {
        try {
            System.out.println("############################################## Start Time : " + getCurrentData());
            // 1. URL 선언
            String connUrl = "http://www.daum.net";
           
            // 2. HTML 가져오기
            Connection conn = Jsoup
                    .connect(connUrl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .userAgent(USER_AGENT)
                    .method(Connection.Method.GET)
                    .ignoreContentType(true);
           
            Document doc = conn.get();
           
            // 3. 가져온 HTML Document 를 확인하기
            //System.out.println(doc.toString());
            System.out.println(doc.select(".list_stxt").toString());
           
        } catch (IOException e) {
            // Exp : Connection Fail
            e.printStackTrace();
        }
        System.out.println("############################################## End Time : " + getCurrentData());
    }
}

 

반응형

+ Recent posts