Jsoup에서 라이브러리 다운받기
라이브러리를 프로젝트에 추가한다.
connect 메서드로 연결할 사이트의 url을 파라미터로 넘겨준다.
select 메서드에서 찾고자 하는 값의 위치를 입력한다.
Pattern | Matches | Example |
---|---|---|
* | any element | * |
tag | elements with the given tag name | div |
ns|E | elements of type E in the namespace ns | fb|name finds <fb:name> elements |
#id | elements with attribute ID of "id" | div#wrap , #logo |
.class | elements 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 expression | img[src~=(?i)\\.(png|jpe?g)] |
The above may be combined in any order | div.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;
결과