Java를 이용하여 페이지 정보 가져오기(IP, Header, 페이지정보 등)

2016. 12. 29. 10:12language/java

Java를 이용하여 페이지 정보 가져오기(IP, Header, 페이지정보 등)

 

java를 이용해서 특정 페이지의 Ip와 Header 정보 및 페이지정보를 가져오는 방법에 대해서 포스팅이다.

 

 

* ip 주소와 Header 정보, 페이지 Html 소스 정보 총 3개를 가져오는 방법이다.

 

 

요약

1. ip 주소 가져오기

2. Header 정보 가져오기

3. Html 소스 가져오기

 



 

1. InetAddress 클래스를 이용하여 IP 주소를 가져오기

자신의 ip정보와 네이버의 ip정보를 가져오는 방법이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 자신의 컴퓨터의 IP주소를 얻어오기
try {
    InetAddress localHost = InetAddress.getLocalHost();
    System.out.println("자신의 IP 정보");
    System.out.println("localHost.getHostName() : " + localHost.getHostName());
    System.out.println("localHost.getHostAddress() : " + localHost.getHostAddress());
    System.out.println();
 
    // 네이버의 ip정보 가져오기
    InetAddress byName = InetAddress.getByName("www.naver.com");
    System.out.println("네이버의 IP 정보");
    System.out.println("byName.getHostName() : " + byName.getHostName());
    System.out.println("byName.getHostAddress() : " + byName.getHostAddress());
    System.out.println();
 
    // 네이버의 ip정보 다 가져오기
    InetAddress[] allByName = InetAddress.getAllByName("www.naver.com");
    System.out.println("네이버의 IP 정보들");
    for (InetAddress address : allByName) {
        System.out.println("address.getHostName() : " + address.getHostName());
        System.out.println("address.getHostAddress() : " + address.getHostAddress());
    }
catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
cs

 

 

결과 :

자신의 IP 정보
localHost.getHostName() : khkim-PC
localHost.getHostAddress() : 000.000.000.000

네이버의 IP 정보
byName.getHostName() : www.naver.com
byName.getHostAddress() : 125.209.222.141

네이버의 IP 정보들
address.getHostName() : www.naver.com
address.getHostAddress() : 125.209.222.141
address.getHostName() : www.naver.com
address.getHostAddress() : 202.179.177.22

 

 

 

 

 

2. URLConnection 을 사용하여 특정 페이지의 Header 정보를 가져오기

특정 페이지의 header 정보를 iterator을 사용하여 모두 출력하는 방법이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// URLConnection : header 정보를 가지고 온다.
try {
    URL url = new URL("http://www.naver.com");
 
    URLConnection urlCon = url.openConnection();
 
    // InputStream : 해당 호스트의 페이지 정보를 가져온다.
    System.out.println("urlCon.getContentType() : " + urlCon.getContentType());
    System.out.println("urlCon.getContent() : " + urlCon.getContent());
    System.out.println("urlCon.getContentEncoding() : " + urlCon.getContentEncoding());
    Map<String, List<String>> map = urlCon.getHeaderFields();
 
    Iterator<String> iterator = map.keySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        System.out.println("Header Info : " + key + " = " + map.get(key));
    }
 
catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
cs

 

 

결과 :

urlCon.getContentType() : text/html; charset=UTF-8
urlCon.getContent() : sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@472a2a50
urlCon.getContentEncoding() : null
Header Info : null = [HTTP/1.1 200 OK]
Header Info : X-Frame-Options = [SAMEORIGIN]
Header Info : Transfer-Encoding = [chunked]
Header Info : Date = [Thu, 29 Dec 2016 00:58:14 GMT]
Header Info : P3P = [CP="CAO DSP CURa ADMa TAIa PSAa OUR LAW STP PHY ONL UNI PUR FIN COM NAV INT DEM STA PRE"]
Header Info : Connection = [close]
Header Info : Content-Type = [text/html; charset=UTF-8]
Header Info : Server = [nginx]
Header Info : Pragma = [no-cache]
Header Info : Cache-Control = [no-cache, no-store, must-revalidate]

 

 



 

3. URL.openStream()을 이용하여 페이지 정보 가져오기

페이지의 html 소스를 text 방식으로 출력하는 방법입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
try {
    URL url = new URL("http://www.naver.com");
 
    InputStream openStream = url.openStream();
    InputStreamReader isr1 = new InputStreamReader(openStream, "UTF-8");
    BufferedReader bis1 = new BufferedReader(isr1);
    System.out.println("-------------------------------------------------------");
    System.out.println("페이지정보 : ");
    while (true) {
        String str = bis1.readLine(); // 한줄을 읽어서
        if (str == null) {
            break;
        }
        System.out.println(str);
    }
 
    bis1.close();
    isr1.close();
 
catch (MalformedURLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
cs

 

 

결과 :

페이지의 정보 (Html 소스)를 Text 형식으로 가져옴

 

* 너무 많아서 생략

 

가공하면 크롤링에 수월하게 사용할 수 있음