Building Android Application HTML Parsing using JSOUP
This is one of my experiences about how to create android application using JSOUP. JSOUP is a java library which can parse any HTML tag from web page.
Why using JSOUP?
The name of my application is indochord, which showing list of recent chord regurarly. But I dont have any data on server. List of chord available on different website, and thoose site not provide an API. So for alternative way, I parse the HTML page from www.chordfrenzy.com.
How I can do it?
First step, go to www.chordfrenzy.com and looking for recent chord table. Inspect this element using firebug (Mozilla add ons), we know the name of recent chord table and its parameter.
From picture above, we have a part of HTML tag like this
<div class=“box” style=“height:595px”>
<div class=“boxtitlebox”>
<div class=“boxtitle”><h4>13 RECENT CHORDS</h4></div><div class=“clear”></div>
</div>
<div class=“listitem”><a href=“https://www.chordfrenzy.com/chord/8945/tangga-satu-bendera-kord-lirik-lagu”>
<div class=“subtitle”>Chord Tangga</div>
<div class=“title”>Satu Bendera</div>
</a></div>
<div class=“listitem”><a href=“https://www.chordfrenzy.com/chord/7943/jkt48-first-rabbit-kelinci-pertama-kord-lirik-lagu”> <div class=“subtitle”>Chord JKT48</div>
<div class=“title”>First Rabbit (Kelinci Pertama)</div>
</a></div>
<div class=“listitem”><a href=“https://www.chordfrenzy.com/chord/4969/ada-band-beib-kord-lirik-lagu”>
<div class=“subtitle”>Chord Ada Band</div>
<div class=“title”>Beib</div>
</a></div>
<div class=“listitem”><a href=“https://www.chordfrenzy.com/chord/8906/setia-band-jangan-ngarep-kord-lirik-lagu”>
<div class=“subtitle”>Chord Setia Band</div>
<div class=“title”>Jangan Ngarep</div>
</a></div>
……
</div>
I want parse each item contain ahref, subtitle and title. So with JSOUP we can do
Document document = Jsoup.connect(“www.chordfenzy.comâ€).timeout(10000).get();
Element d = document.select(“div.box”).first();
Elements subtitles = d.getElementsByClass(“subtitle”); //result : Chord Tangga
Elements titles = d.getElementsByClass(“title”);//result : atu Bendera
Element elementlink : d.select(“a[href]”)
String alink = elementlink.attr(“href”)//result : https://www.chordfrenzy.com/chord/8945/tangga-satu-bendera-kord-lirik-lagu
For more detail, we can read JSOUP documentation here.
Comments
Leave a Comment