Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access nested divs using Jsoup

Tags:

java

html

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.

like image 716
Tweety Avatar asked Apr 29 '26 06:04

Tweety


2 Answers

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();
like image 63
Francis Toth Avatar answered Apr 30 '26 19:04

Francis Toth


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!

like image 40
Syrion Avatar answered Apr 30 '26 21:04

Syrion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!