2017-09-25 51 views
1

我有這樣如何使用jsoup從html文件中的強標記檢索數據?

<div class="bs-example"> 
    <div class="panel panel-primary"> 
    <div class="panel-heading"> 
     <h3 class="panel-title">ABC</h3> 
    </div> 
    <div class="panel-body"> 
     <div class="slimScroller" style="height:280px; position: relative;" data-rail-visible="1" data-always-visible="1"> 
     <strong>Name:</strong> 
     <a href="https://ABC"> </a><br /> 
     <strong>ID No:</strong> XXXXX<br /> 
     <strong>Status:</strong> ACTIVE<br /> 
     <strong>Class:</strong> 5<br /> 
     <strong>Category:</strong> A<br /> 
     <strong>Marks:</strong> 500<br /> 
     </div> 
    </div> 
    </div> 
</div> 

一些HTML數據我想爲(多學生數據)輸出:

Name: ABC 
ID No.: XXXXX 
Status: Active 
Class: 5 
Category: A 
Marks: 500 

如何使用jsoup或任何其他方式獲得這些數據?請幫忙。

回答

0

您可以使用Element.nextElementSibling()或/和Element.nextSibling()來獲取所需的輸出。

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class Exam { 
    public static void main(String[] args) { 
    String html = "<div class=\"bs-example\">" + 
        " <div class=\"panel panel-primary\">" + 
        " <div class=\"panel-heading\">" + 
        "  <h3 class=\"panel-title\">ABC</h3>" + 
        " </div>" + 
        " <div class=\"panel-body\">" + 
        "  <div class=\"slimScroller\" style=\"height:280px; position: relative;\" data-rail-visible=\"1\" data-always-visible=\"1\">" + 
        "  <strong>Name:</strong>" + 
        "  <a href=\"https://ABC\"> </a><br />" + 
        "  <strong>ID No:</strong> XXXXX<br />" + 
        "  <strong>Status:</strong> ACTIVE<br />" + 
        "  <strong>Class:</strong> 5<br />" + 
        "  <strong>Category:</strong> A<br />" + 
        "  <strong>Marks:</strong> 500<br />" + 
        "  </div>" + 
        " </div>" + 
        " </div>" + 
        "</div>"; 

    Document doc = Jsoup.parse(html); 
    Elements eles = doc.select("div.slimScroller strong"); 
    for(Element e :eles) 
    System.out.println(e.text() + 
         (e.nextElementSibling().tagName().equals("a")? 
         e.nextElementSibling().attr("href").replace("https://", ""): 
         e.nextSibling().toString())); 
    } 
} 
+0

嗨厄立特里亞,真的很感激的幫助......但是,這將引發一個空指針異常的(e.nextElementSibling()。tagName()。equals(「a」)? –

+0

您是否嘗試過使用您的原始html代碼段或由@anurag編輯的代碼段?您的第一個發佈代碼段中可能缺少某些內容。包括在代碼中我沒有得到一個NPE – Eritrean

+0

我在原始代碼片段上試了這個,我的「a」標籤的內容如下:名稱:ABC

0

下面的代碼應提供輸出基於指定了你的評論,說明您的a標籤如何:

private static void printStudentInfo(Document document){ 
    Elements students = document.select("div.slimScroller strong"); 

    for(Element student : students){ 
     System.out.print(student.text()); 

     System.out.println(student.nextElementSibling().tagName().equals("a") ? 
       student.nextElementSibling().text() : student.nextSibling().toString()); 
    } 
} 
相關問題