Tweety
Tweety

Reputation: 55

How to access nested divs using Jsoup

This is the html page:

<div class="doc_details">
  <fieldset style="border: 0pt">
    <div class="row">
      <div class="col-sm-6 col-md-6">
        <div class="row">
          <div class="col-sm-6 col-md-6">
            <b>Speciality</b>
          </div>
          <div class="col-sm-6 col-md-6">ABCD</div>
        </div>
        <div class="row">
          <div class="col-sm-6 col-md-6">
            <b>City</b>
          </div>
          <div class="col-sm-6 col-md-6">Ranchi</div>
        </div>
        <div class="row">
          <div class="col-sm-6 col-md-6">
            <b>Residence Address</b>
          </div>
          <div class="col-sm-6 col-md-6">Ranchi</div>
        </div>
        <div class="row">
          <div class="col-sm-6 col-md-6">
            <b>Business Address</b>
          </div>
          <div class="col-sm-6 col-md-6">Ranchi</div>
        </div>
      </div>
    </div>
  </fieldset>
</div>

I would like to access only the values of the Speciality, city and address columns into a variable as follows:

Elements rows = doc.select("div.doc_details div.row div.row ");
Element row_div = rows.select("div.row").get(0);
doctor.speciality = row_div.select("div:eq(0)").text();

But even if I change the get(0) to get(1), I'm not able to get only the values in the variable.

Upvotes: 3

Views: 4859

Answers (3)

Francis Toth
Francis Toth

Reputation: 1685

You can probably do this with a css-selector :

doc.select("div.row > div.col-sm-6:nth-child(2)")

which returns this :

0 = {Element@754} "<div class="col-sm-6 col-md-6">\n  ABCD \n</div>"
1 = {Element@756} "<div class="col-sm-6 col-md-6">\n  Ranchi \n</div>"
2 = {Element@758} "<div class="col-sm-6 col-md-6">\n  Ranchi \n</div>"
3 = {Element@760} "<div class="col-sm-6 col-md-6">\n  Ranchi \n</div>"

It's then really up to you, you can for example map the list to the text of each div :

    divs.stream().map(new Function<Element, String>() {
        @Override
        public String apply(Element element) {
            return element.text();
        }
    }).collect(Collectors.toList()));

or more simple :

String speciality = divs.get(0).text();
String city = divs.get(1).text();
String adress = divs.get(2).text();

Upvotes: 3

luksch
luksch

Reputation: 11712

Here is how I would do it:

Document doc = Jsoup.parse(html);
Elements rows = doc.select("div.doc_details div.row div.row ");
for (Element row : rows){
    Elements innerDivs = row.select("div");
    String header = innerDivs.get(1).text();
    String content = innerDivs.get(2).text();
    System.out.println("header = "+header+ " -> "+content);
}

I think you were mistaken in the css selector!

Edit: (thanks to the OP the indexes are correct now)

Upvotes: 1

Syrion
Syrion

Reputation: 199

Try this:

Elements rows = doc.select("div.doc_details div.row div.row ");
Element row_div = rows.select("div.col-sm-6").get(1);
doctor.speciality = row_div.text();

tell me if it works!

Upvotes: 1

Related Questions