Jsoup从div类中获取文本

时间:2015-11-27 21:18:00

标签: android html jsoup

这是我的HTML

<div class="open-statuses">

  <div class="open-status" id="lifts-status-scripted">
    <h3>Lifts</h3>
    <div class="status-graph">
      <canvas width="177" height="177"></canvas>
      <div class="open-number">04</div>
      <div class="total-number">4</div>
    </div>
    <a href="/lifts-and-trails/" class="details">Details</a>
  </div>
  <div class="open-status" id="trails-status-scripted">
    <h3>Trails</h3>
    <div class="status-graph">
      <canvas width="177" height="177"></canvas>
      <div class="open-number">12</div>
      <div class="total-number">169</div>
    </div>
    <a href="/lifts-and-trails/" class="details">Details</a>
  </div>

  <div class="open-status open" id="road-status-scripted">
    <h3>Road</h3>
    <div class="status-graph">
      <canvas width="177" height="177"></canvas>
      <div class="status-message">Open</div>
    </div>
    <a href="/mountain-report/road-conditions/" class="road-conditions">Road Conditions</a>
  </div>
</div>

我需要来自(div class =&#34; open-status&#34; id =&#34; trails-status-scripted&#34;)的文本,我无法做到。我将这段代码用于第一堂课,没有任何问题,但我不能为第二堂课做这件事。

Elements div1=document.select("#mountain-report-page");
Elements div2=div1.select(".open-statuses-holder");
Elements div3=div2.select(".open-statuses");
Jliftbig = div3.select("div.open-number").first().ownText();

有任何线索吗?

3 个答案:

答案 0 :(得分:0)

以这种方式简化:

Elements div = doc.select("div[id=mountain-report-page] div[class=open-statuses-holder] div[class=open-statuses] div[class=open-status]");

for (Element e : div){
    if (e.id().equals("trails-status-scripted")){
        Element ele = e.select("div[class=status-graph] div[class=open-number]").first();
        String str = ele.text();
    }
}

答案 1 :(得分:0)

完成。我用这段代码解决了

Element div = document.select("div[id=mountain-report-page] div[class=open-statuses-holder] div[class=open-statuses] div[class=open-status] ").get(2);
String Jtrails = div.select("div.open-number").first().ownText();

答案 2 :(得分:0)

由于HTML中包含所有ID must be unique,因此您只需使用此选择器即可。

#foo

备注:

  • *[id=foo]等于.foo
  • *[class=foo]等于{{1}}