-
002. HTML 파싱하기 - 태그읽기Java 2015. 7. 29. 15:511. getElementsByTag(String tag)를 이용한 태그읽기123456789101112131415161718192021222324252627282930313233343536373839404142import java.io.File;import java.io.IOException;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;// 파일에 있는 HTML 파싱하기public class Jsoup05 {public static void main(String[] args) {String fileName = "Jsoup05.html";File file = new File(fileName);Document document = null;try {document = Jsoup.parse(file, "UTF-8");// 태그 읽기Elements links1 = document.getElementsByTag("h1");for (Element link : links1) {System.out.println(link.text());}System.out.println();// 태그 읽기Elements links2 = document.getElementsByTag("h2");for (Element link : links2) {System.out.println(link.text());}} catch (IOException e) {e.printStackTrace();}}}
cs Jsoup05.html
123456789101112131415161718192021<!doctype html><html lang="ko"><head><meta charset="UTF-8"><title>연습용 HTML문서</title></head><body><h1>파일로부터 h1 tag 읽기 1</h1><h2>파일로부터 h2 tag 읽기 1</h2><h1>파일로부터 h1 tag 읽기 2</h1><h2>파일로부터 h2 tag 읽기 2</h2><h1>파일로부터 h1 tag 읽기 3</h1><h2>파일로부터 h2 tag 읽기 3</h2><h1>파일로부터 h1 tag 읽기 4</h1><h2>파일로부터 h2 tag 읽기 4</h2></body></html>cs 결과
123456789101112파일로부터 h1 tag 읽기 1파일로부터 h1 tag 읽기 2파일로부터 h1 tag 읽기 3파일로부터 h1 tag 읽기 4파일로부터 h2 tag 읽기 1파일로부터 h2 tag 읽기 2파일로부터 h2 tag 읽기 3파일로부터 h2 tag 읽기 4cs 2. select(tagName)으로 태그 읽기
123456789101112131415161718192021222324252627282930313233343536import java.io.File;import java.io.IOException;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;// 파일에 있는 HTML 파싱하기public class Jsoup06 {public static void main(String[] args) {String fileName = "Jsoup05.html";File file = new File(fileName);Document document = null;try {document = Jsoup.parse(file, "UTF-8");// select로 태그 읽기Elements links1 = document.select("h1");for (Element link : links1) {System.out.println(link.text());}System.out.println();Elements links2 = document.select("h2");for (Element link : links2) {System.out.println(link.text());}} catch (IOException e) {e.printStackTrace();}}}cs 결과
123456789101112파일로부터 h1 tag 읽기 1파일로부터 h1 tag 읽기 2파일로부터 h1 tag 읽기 3파일로부터 h1 tag 읽기 4파일로부터 h2 tag 읽기 1파일로부터 h2 tag 읽기 2파일로부터 h2 tag 읽기 3파일로부터 h2 tag 읽기 4cs 3. 자식 테그를 검색하기
1234567891011121314151617181920212223242526272829303132333435363738import java.io.IOException;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;// 뉴스 RSS에서 읽기public class Jsoup07 {public static void main(String[] args) {Document document = null;try {String url = "http://rss.hankyung.com/new/news_main.xml";document = Jsoup.connect(url).get();Elements elements = document.getElementsByTag("item");for(Element el : elements){Elements titles = el.getElementsByTag("title");for(Element title : titles){String temp = title.text().replace("<![CDATA[","").replace("]]>","");System.out.println(temp);}}System.out.println();elements.clear();elements = document.select("channel item title");for(Element el : elements){String temp = el.text().replace("<![CDATA[","").replace("]]>","");System.out.println(temp);}} catch (IOException e) {e.printStackTrace();}}}cs 4. id, class로 검색하기
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import java.io.File;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 Jsoup08 {public static void main(String[] args) {String fileName = "Jsoup08.html";File file = new File(fileName);Document document = null;try {document = Jsoup.parse(file, "UTF-8");// getElementById로 태그 읽기// 리턴값이 Element타입이다.Element box1 = document.getElementById("box1");System.out.println(box1.text());System.out.println();// select("#아이디")로 읽기// 리턴값이 Elements타입이다.Elements box2 = document.select("#box2");for (Element box : box2) {System.out.println(box.text());}System.out.println();// getElementsByClass로 태그 읽기Elements redbox = document.getElementsByClass("redbox");System.out.println("개수 : " + redbox.size() + "개");for (Element box : redbox) {System.out.println(box.text());}System.out.println();// select(".클래스")로 태그 읽기Elements greenbox = document.select(".greenbox");System.out.println("개수 : " + greenbox.size() + "개");for (Element box : greenbox) {System.out.println(box.text());}System.out.println();} catch (IOException e) {e.printStackTrace();}}}cs Jsoup08.html
1234567891011121314151617181920212223242526272829303132<!doctype html><html lang="ko"><head><meta charset="UTF-8"><title>연습용 HTML문서</title><style type="text/css">.redbox {border: 1px solid red;margin: 5;}.greenbox {border: 1px solid red;margin: 5;}</style></head><body><div id="box1" class="greenbox">나는 id가 box1인 영역입니다. class는 greenbox</div><div id="box2" class="redbox">나는 id가 box2인 영역입니다. class는 redbox</div><div id="box2" class="greenbox">나는 id가 box2인 영역입니다. class는 greenbox</div></body></html>cs 'Java' 카테고리의 다른 글
005. HTML 파싱하기 - 네이버 실시간 급상승 검색어 (0) 2015.07.30 004. HTML 파싱하기 - 다음 실시간 이슈 (0) 2015.07.30 003. HTML 파싱하기 - 속성읽기 (0) 2015.07.29 001. HTML 파싱하기 - 준비하기 (0) 2015.07.29 QR코드 생성하기 (0) 2015.07.29 댓글