0

我有一个试图从中挖掘Logname价值的 HTML 页面。我可以将所有li文本作为一个字符串组合在一起,但不是我想要的。我只想要li.logname 之后的第二部分</span>。有什么方法可以轻松搞定?有了我所拥有的,我可以进行拆分并得到我想要的,但似乎应该有一种更优雅的方式?

当前代码

Elements detail = mHtml.select ("div.alpha-first");


        for (Element items : detail)
        {
            Log.d (TAG, " label text " + items.text());

            detail.

            if (items.text().equals ("ACID"))
            {
                Log.d (TAG, " got ACID ");
            }

        }

HTML

<html>
<title>emp id chart</title>
<body>
<div class="alpha-first">
      <ul class="account-detail">
         <li><span class="label">ID</span>42</li>
         <li><span class="label">Logname</span>George</li>
         <li><span class="label">Surname</span>Glass</li>
         <li><span class="label">ACID</span>15</li>
         <li><span class="label">Dept</span>101348</li>
         <li><span class="label">Empclass</span>Echo</li>
      </ul>
      <p class="last-swipe">3 Apr 9:53</p><br>  </div>
   <div class="detail-last-loc">
      <p style="font-size: 8pt;">Current status</p>
      <p class="current-location">Bldg #23 South Lot</p>
      <p> current time 10:43 <br /></p>
      <div class="detail-extra">
         <p><a href="/empswipe/history/151034842">More</a> | <a href="/empswipe/history/151034842/3">3 Day History</a></p>
      </div>
</div>
</body>
</html>
4

1 回答 1

3

据我了解,以您的示例为例,您将希望从: 获得<li><span class="label">Logname</span>George</li>值:George

真的不需要迭代,直接搞定。我不会把这段代码称为优雅,但仍然是:

    //Select the <span> element the text "Logname"
    Elements select = mHtml.select(".account-detail span.label:contains(Logname)");

    //Get the element itself, since the select returns a list
    Element lognameSpan = select.get(0);

    //Get the <li> parent of the <span>
    Element parent = lognameSpan.parent();

    //Access the text node of the <li> directly since there is only one
    String logname = parent.textNodes().get(0).text();

希望能帮助到你。

于 2013-04-03T18:12:55.867 回答