Pawan
Pawan

Reputation: 32321

How to Parse through Contents of extracted HTML

From a Website i am getting the following content Using Jsoup Call .

<body>
 <div align="center" class="main">
  <div id="box1">
   <div id="heading" align="center">
    SENSEX
   </div>
   <div id="feeds" align="center">
    23,410.18
   </div>
   <div id="feeds_se" align="center">
    <img src="arrow_red.png" alt="" width="12" height="13" /> -378.61
   </div>
  </div>
  <div id="box2"></div>
 </div>
 <div align="center" class="main">
  <div id="box1">
   <div id="heading" align="center">
    USD/INR
   </div>
   <div id="feeds" align="center">
    68.50
   </div>
   <div id="feeds_se" align="center">
    <img src="arrow_red.png" alt="" width="12" height="13" /> -0.05
   </div>
  </div>
  <div id="box2"></div>
 </div>
 <div align="center" class="main">
  <div id="box1">
   <div id="heading" align="center">
    Crude($/BBL)
   </div>
   <div id="feeds" align="center">
    34.69
   </div>
   <div id="feeds_se" align="center">
    <img src="arrow_green.png" alt="" width="12" height="13" /> 1.68
   </div>
  </div>
  <div id="box2"></div>
 </div>

</body>

Could you please let me know how can i read through these values

How can i retrive the values

SENSEX   23,410.18  -378.61
USD/INR   68.50    -0.05
Crude($/BBL)  34.69   1.68

Upvotes: 0

Views: 54

Answers (2)

Sestertius
Sestertius

Reputation: 1372

Try this code:

    Document doc = Jsoup.parse(yourHtmlString);
    Elements elements = doc.select("div.main");
    for (Element element : elements) {
        System.out.println(element.getElementById("heading").text() + " "
        + element.getElementById("feeds").text()+ " "
        + element.getElementById("feeds_se").text());       
    }

Output:

SENSEX 23,410.18 -378.61
USD/INR 68.50 -0.05
Crude($/BBL) 34.69 1.68

Upvotes: 1

luksch
luksch

Reputation: 11712

To get to the information in HTML pages, you can use CSS selectors. In your example, you could do this:

Elements els = doc.select("#heading");
for (Element el : els){
    System.out.println(el.text());
}

Note that your html is not really valid, since it contains non-unique ids. This should not happen in HTML, but fortunately Jsoup does not care about this.

Upvotes: 0

Related Questions