본문 바로가기

컴퓨터/JAVA

Jsoup로 파싱하기

Jsoup에서 라이브러리 다운받기

http://jsoup.org/download


라이브러리를 프로젝트에 추가한다.



connect 메서드로 연결할 사이트의 url을 파라미터로 넘겨준다.

select 메서드에서 찾고자 하는 값의 위치를 입력한다.

PatternMatchesExample
*any element*
tagelements with the given tag namediv
ns|Eelements of type E in the namespace nsfb|name finds <fb:name> elements
#idelements with attribute ID of "id"div#wrap, #logo
.classelements with a class name of "class"div.left, .result
[attr]elements with an attribute named "attr" (with any value)a[href], [title]
[^attrPrefix]elements with an attribute name starting with "attrPrefix". Use to find elements with HTML5 datasets[^data-], div[^data-]
[attr=val]elements with an attribute named "attr", and value equal to "val"img[width=500], a[rel=nofollow]
[attr^=valPrefix]elements with an attribute named "attr", and value starting with "valPrefix"a[href^=http:]
[attr$=valSuffix]elements with an attribute named "attr", and value ending with "valSuffix"img[src$=.png]
[attr*=valContaining]elements with an attribute named "attr", and value containing "valContaining"a[href*=/search/]
[attr~=regex]elements with an attribute named "attr", and value matching the regular expressionimg[src~=(?i)\\.(png|jpe?g)]
The above may be combined in any orderdiv.header[title]



다운로드 수를 파싱해 보겠습니다.


<div class="bylist">

<ul>

<li>

<em><img src="/userpoc/images/detail/txt_downnumb.gif" alt="다운로드 수" /></em>

<span class="type01" title="81">81</span>

</li>

<li>

<em><img src="/userpoc/images/detail/txt_uselevel.gif" alt="이용등급" /></em>

<span title="전체이용가">전체이용가</span>

</li>

<li>

<em><img src="/userpoc/images/detail/txt_adddate.gif" alt="등록일" /></em>

<span title="2013-08-23">2013-08-23</span>

</li>

<li>

<em><img src="/userpoc/images/detail/txt_updaedate.gif" alt="업데이트일" /></em>

<span title="2013-08-27">2013-08-27</span>

</li>

<li>

<em><img src="/userpoc/images/detail/txt_capacity.gif" alt="용량" /></em>

<span title="2000">2,000 KB</span>

</li>

<li>

<em><img src="/userpoc/images/detail/txt_platform.gif" alt="플랫폼" /></em>

<span title="Android">Android</span>

</li>

</ul>

</div>


다운로드 수는 div안에 li 안에 span에 값이 있다. 

그래서 selector을 doc.select("div.bylist ul li span.type01")으로 했다.



예제

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;


public class getParser {
public static void main(String[] args)throws Exception{
Document doc = Jsoup.connect("http://www.tstore.co.kr/userpoc/game/viewProduct.omp?t_top=DP000504&dpCatNo=DP04001&insDpCatNo=DP04001&insProdId=0000413004&prodGrdCd=PD004401&stPrePageNm=DP25002&stActionPositionNm=06&stDisplayOrder=1").get();
Elements titles = doc.select("div.bylist ul li span.type01");
for(Element e: titles){
System.out.println("download: " + e.text());
}
}
}

결과



'컴퓨터 > JAVA' 카테고리의 다른 글

JSON  (0) 2013.09.06
강제로 예외 발생시키기  (0) 2013.08.30
ArrayList  (0) 2013.07.05
super  (0) 2013.07.05
this  (0) 2013.07.05