你如何在div类中的div类中获得div类?

时间:2013-09-29 13:54:37

标签: java android parsing selector jsoup

我的问题是我需要在div类中的div类中获取div类,并且是具有相同名称但不同数据的类的4个实例... 我现在可以在div类中获得第一个div类,但我需要能够访问其中的其他元素......例如:

docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
Elements tideTableRows = docTide.select("div.tide_row.odd");
Element firstDiv = tideTableRows.first();
Element secondDiv = tideTableRows.get(1);
System.out.println("This is the first div: " + firstDiv.text());
System.out.println("This is the second div: " + secondDiv.text()); 

但这是网页的结构,其中有2个重复,我需要访问它们中的每一个,例如:

 <div class="tide_row odd"> 
  <div class="time">00:57</div>
  <div class="height_m">4.9</div>
  <div class="height_f">16,1</div>
  <div class="range_m">1.9</div>
  <div class="range_f">6,3</div>
  </div>
 <div class="tide_row even">
  <div class="time">07:23</div>
  <div class="height_m">2.9</div>
  <div class="height_f">9,6</div>
  <div class="range_m">2</div>
  <div class="range_f">6,7</div>
  </div>
  <div class="tide_row odd">
  <div class="time">13:46</div>
  <div class="height_m">5.1</div>
  <div class="height_f">16,9</div>
  <div class="range_m">2.2</div>
  <div class="range_f">7,3</div>
  </div>
  <div class="tide_row even">
  <div class="time">20:23</div>
  <div class="height_m">2.8</div>
  <div class="height_f">9,2</div>
  <div class="range_m">2.3</div>
  <div class="range_f">7,7</div>
  </div>

所以基本上它在具有相同名称的单独类中嵌套了类,如何构造正确的语法以分别从类中返回数据? 这很难解释!

编辑:这就是我设法从嵌套类中提取信息的方法:

 docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
                         Elements tideTimeOdd = docTide.select("div.tide_row.odd div:eq(0)");
                         Elements tideTimeEven = docTide.select("div.tide_row.even div:eq(0)");
                         Elements tideHightOdd = docTide.select("div.tide_row.odd div:eq(2)");
                         Elements tideHightEven = docTide.select("div.tide_row.even div:eq(2)");
                            Element firstTideTime = tideTimeOdd.first();
                            Element secondTideTime = tideTimeEven.first();
                            Element thirdTideTime = tideTimeOdd.get(1);
                            Element fourthTideTime = tideTimeEven.get(1);

                            Element firstTideHight = tideHightOdd.first();
                            Element secondTideHight = tideHightEven.first();
                            Element thirdTideHight = tideHightOdd.get(1);
                            Element fourthTideHight = tideHightEven.get(1);

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
Elements tideTableRows = docTide.select("div.tide_row");
for (Element tideTableRow : tideTableRows){
  if (tideTableRow.hasClass("odd")){
     //do the odd stuff...
  }
  Elements innerDivs = tideTableRows.select("div");

  for (Element innerDiv : innerDivs){
    //do whatever you need
  }
}

注意:代码未经过测试。

更新:我向您展示了如何只访问奇数行...从那里您应该能够自己完成剩下的工作,我希望如此。

答案 1 :(得分:1)

只需这样做就可以了:

 docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
                            Elements tideTableRows = docTide.select("div[class=tide_row odd]");
                            Element firstDiv += tideTableRows.select("div[class=time]");
                            Element secondDiv += tideTableRows.select("div[class=height]");

如果可以的话,您应该尝试按ID访问元素。它使您的代码更简单,如果您在同一容器中有50个标题,作为示例,这样您就不会必须把它们全部计算在内。

单独元素:

 docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
                            Element tideTableRows = docTide.select("div[class=tide_row odd]").first();
                            Element firstDiv1 = tideTableRows.select("div[class=time]");
                            Element secondDiv1 = tideTableRows.select("div[class=height]");
                            tideTableRows2 = docTide.select("div[class=tide_row odd]").second();
                            Element firstDiv2 = tideTableRows.select("div[class=time]");
                            Element secondDiv2 = tideTableRows.select("div[class=height]");