JAVA HTML Paser (JSOUP)

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

String htmlString = "<table>" +
"<thead>" +
"	<th>" +
"		<td>번호</td>" +
"		<td>제목</td>" +
"	</th>" +
"</thead>" +
"<tbody>" +
"	<tr>" +
"		<td>1</td>" +
"		<td>제목1 입니다.</td>" +
"	</tr>" +
"	<tr>" +
"		<td>1</td>" +
"		<td>제목2 입니다</td>" +
"	</tr>" +
"</tbody>" +
"</table>" + 
"<table class='A'>" +
"<thead>" +
"	<th>" +
"		<td>번호</td>" +
"		<td>제목</td>" +
"	</th>" +
"</thead>" +
"<tbody>" +
"	<tr>" +
"		<td>1</td>" +
"		<td>제목1 입니다.</td>" +
"	</tr>" +
"	<tr>" +
"		<td>2</td>" +
"		<td><span>111</span>제목2 입니다</td>" +
"	</tr>" +
"</tbody>" +
"</table>" + 
"<div id='divTest1'><span>test1</span></div>" + 
"<div id='divTest2'><span>test1</span></div>";

Document doc = Jsoup.parse(htmlString);

// 두번째 테이블 가져오기 (배열형태)
Element element = doc.select("table").get(1);
// 두번째 테이블 가져오기 (class)
// Elements htmlEl = doc.select(".A").toString();

Elements elements = element.select("tbody > tr > td:eq(1)");

for (Element htmlEl : elements){
	logger.info("text : " + htmlEl.text() + ", html : " + htmlEl.html());
}

You may also like...

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다