본문 바로가기

Learning/JSP

포털사이트에서 필요한 정보 출력하기 (jsoup select)

New Dynamic Web Project로 Jsoup_test 생성

com.jsoup.test 패키지에 Test01.java 클래스 생성

 

 

https://mvnrepository.com/artifact/org.jsoup/jsoup/1.11.3

 

Maven Repository: org.jsoup » jsoup » 1.11.3

jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods. jsoup implements the WHATWG HTML5 specification, and parses HTML to the s

mvnrepository.com

에서 jsoup-1.11.3.jar 다운받아서 WEB-INF 내 lib 폴더에 붙여넣기

 

다음 사이트에서 영화 검색, 검색창 주소 복사해서 Jsou.connnet 함수 인자로 넣기


 

Test01.java

package com.jsoup.test;

import java.io.IOException;

import java.util.ArrayList;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.select.Elements;

public class Test01 {

    public static void main(String[] args) {

        try {

            Document doc=Jsoup.connect("https://search.daum.net/search?w=tot&DA=YZR&t__nil_searchbox=btn&sug=&sugo=&sq=&o=&q=%EC%98%81%ED%99%94").get();

//          System.out.println(doc);

            

            Elements movie_list=doc.select("ol.movie_list li");

//          System.out.println(movie_list);

            System.out.println("====제목====");

            Elements titleContainer=movie_list.select("div.info_tit a");

//          System.out.println(titleContainer);

//          for(int i=0;i<titleContainer.size();i++) {

//              System.out.println(titleContainer.get(i).text());

//          }

//          

//          System.out.println("====평점====");

            Elements scoreContainer=movie_list.select("em.rate");

//          for(int i=0;i<scoreContainer.size();i++) {

//              System.out.println(scoreContainer.get(i).text());

//          }

            

            ArrayList<Movie> arr=new ArrayList<Movie>();

            for(int i=0;i<scoreContainer.size();i++) {

                Movie movie=new Movie();

                String title=titleContainer.get(i).text();

                String rate=scoreContainer.get(i).text();

                movie.setTitle(title);

                movie.setRate(rate);

                arr.add(movie);

            }

            for(Movie movie:arr) {

                System.out.println("제목:"+movie.getTitle());

                System.out.println("평점: "+movie.getRate());

                System.out.println();

            }

            

        } catch (IOException e) {

            e.printStackTrace();

        }

        

    }

}

Movie.java

 

package com.jsoup.test;

public class Movie {

    private String title;

    private String rate;

    

    public String getTitle() {

        return title == null ? "" : title.trim();

    }

    public void setTitle(String title) {

        this.title = title;

    }

    public String getRate() {

        return rate == null ? "" : rate.trim();

    }

    public void setRate(String rate) {

        this.rate = rate;

    }

}

 


손흥민의 프로필 출력하기

 

Son.java

 

package com.jsoup.test;

import java.io.IOException;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.select.Elements;

public class Son {

    public static void main(String[] args) {

        try {

            Document doc=Jsoup.connect("https://search.daum.net/search?w=tot&DA=YZR&t__nil_searchbox=btn&sug=&sugo=&sq=&o=&q=%EC%86%90%ED%9D%A5%EB%AF%BC").get();

            Elements container=doc.select("div.type_thumb_s160 dl");

            Elements title=doc.select("div.type_thumb_s160 dl dt");

            Elements data=doc.select("div.type_thumb_s160 dl dd");

            System.out.println("손흥민에 대한 정보");

            for(int i=0;i<container.size();i++) {

                String titleText=title.get(i).text();

                String dataText=data.get(i).text();

                System.out.println(titleText+":"+dataText);

            }

        

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

 

 


 

최근 로또 5회차의 당첨번호 출력하기

 

Lottery2.java

package com.jsoup.test;

import java.io.IOException;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;

public class Lottery2 {

    public static void main(String[] args) {

        Document doc;

        try {

            doc=Jsoup.connect("https://m.dhlottery.co.kr/common.do?method=main").get();

            Elements lottoNo=doc.select("#lottoDrwNo"); //회차

            Elements lottoDrwNo=doc.select("div.prizeresult"); //로또번호

        

            for(int i=0;i<lottoNo.size();i++) {

                System.out.println(lottoNo.get(i).text()+" 당첨번호"); //회차

                //회차에 해당하는 로또 번호

                System.out.println(lottoDrwNo.get(i).select("span").text());

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}



 

환율 출력하기

 

Exchage.java

 

package com.jsoup.test;

import java.io.IOException;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;

public class Exchage {

    public static void main(String[] args) {

        Document doc;

        try {

            doc=Jsoup.connect("https://search.naver.com/search.naver?sm=top_hty&fbm=1&ie=utf8&query=%ED%99%98%EC%9C%A8").get();

            Elements table=doc.select("table.rate_table_info");

            Elements tr=table.select("tbody tr");

            for(int i=0;i<tr.size();i++) {

                String country=tr.get(i).select("th a").text();

                String ratio=tr.get(i).select("td").first().text();

                System.out.println(country+"=>"+ratio);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}