2016-10-25 146 views
1

我想解析一個表,使用jsoup。我試圖獲取航班數據,但沒有成功!解析JSOUP表

我的代碼是:

try { 
    doc = Jsoup.connect("a.html").timeout(13 * 1000).get(); 
    Element table = doc.select("table.arrive-depart-table tbody tr").first(); 
    Iterator<Element> iterator = table.select("td").iterator(); 
    Log.d("log", iterator.next().text()); 
} 

,這是HTML:

<table class="arrive-depart-table"> 
    <tbody> 
    <tr> 
     <td> string 1</td> 
     <td> string 2</td> 
     <td> string 3</td> 
     <td> string 4</td> 
     <td> string 5</td> 
    </tr> 
<tr> 
<td> string 6</td> 
...and more 

例外:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.jsoup.select.Elements org.jsoup.nodes.Element.select(java.lang.String)' on a null object reference 

我無法解析表飛行。

謝謝

+0

問題是什麼?發佈網址,預期輸出和當前輸出。還要確保html源代碼包含禁用JavaScript的數據。 –

+0

嗨,我編輯了第一篇文章 – 0x00

+0

這個「到達 - 離開表」類是javascript的fillup,所以你需要使用htmlunit驅動程序或硒 –

回答

1

使用背景AJAX調用(檢查:Chrome瀏覽器開發工具 - >網絡選項卡)和JSON.simple解析響應,它可能得到的數據沒有JavaScript:

示例代碼

try { 
    String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36"; 
    String url = "https://www.maltairport.com/wp-content/themes/mia/flightsinfo.php?arrivalsDepartures_action=getArrivalsDepartures"; 
    String referer = "https://www.maltairport.com/passenger/flights-landing/arrivals-departures/"; 
    String host = "www.maltairport.com"; 

    Document doc = Jsoup.connect(url).userAgent(userAgent).header("Host", host).header("Referer", referer).ignoreContentType(true).get(); 

    JSONObject jsonObject = (JSONObject) new JSONParser().parse(doc.body().text()); 

    JSONArray arrivals = (JSONArray) jsonObject.get("arrivals"); 
    JSONArray departures = (JSONArray) jsonObject.get("departures"); 

    System.out.println("departures"); 

    for (Object object : departures) { 
     jsonObject = (JSONObject) object; 
     System.out.println("Flight: " + jsonObject.get("flightNumber") + "\n\t" + "To: " + jsonObject.get("airportName") 
       + " SCH: " + jsonObject.get("scheduledTime") + " EST: " + jsonObject.get("estimatedTime") + " Status: " + jsonObject.get("remarks")); 
    } 

    System.out.println("\narrivals"); 

    for (Object object : arrivals) { 
     jsonObject = (JSONObject) object; 
     System.out.println("Flight: " + jsonObject.get("flightNumber") + "\n\t" + "To: " + jsonObject.get("airportName") 
       + " SCH: " + jsonObject.get("scheduledTime") + " EST: " + jsonObject.get("estimatedTime") + " Status: " + jsonObject.get("remarks")); 
    } 

} catch (IOException | ParseException e) { 
    e.printStackTrace(); 
} 

截斷輸出

departures 
Flight: FR 7243 
    To: DUBLIN SCH: 15:15 EST: 15:36 Status: AIRBORNE 
Flight: LS 650 
    To: EAST MIDLANDS SCH: 15:15 EST: 15:33 Status: AIRBORNE  
[...] 

arrivals 
Flight: KL 3399 
    To: AMSTERDAM SCH: 14:50 EST: 14:56 Status: LANDED 
Flight: KM 395 
    To: AMSTERDAM SCH: 14:50 EST: 14:56 Status: LANDED 
[...] 
+0

謝謝你...你是最好的! – 0x00