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());
}
}
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());
}
}
반응형
'개발 > JAVA' 카테고리의 다른 글
표준API 설치시(GPKI 설치시) 서버인증서값 세팅 (0) | 2019.09.16 |
---|---|
Java에서 json 형태로 화면으로 떨구기 (0) | 2019.06.17 |
Java 문제풀이 (0) | 2019.05.27 |
ExecutorService 를 사용하여 스레드수를 제한하여 사용하기 (0) | 2019.03.20 |
java 배열 선언 (0) | 2019.02.22 |