2016-05-29 23 views
-3

我一直在JSoup工作和測試一段時間,這個問題已經困擾了我一段時間。試圖從網站提取信息,但有些值顯示爲空?

http://fx.sauder.ubc.ca/today.html

目前這個類是從本網站應該提取信息從一個表中的唯一的東西,這個程序可以從表中拉是

Code | Currency | fcu/CAD | fcu/USD | Code | Currency | fcu/CAD | fcu/USD 

和所有上顯示的3個字母的代碼該網站,但對於所有其他信息,如價值和美元名稱程序顯示這些爲空。如果任何人想知道px會上升到34,並且ay會上升到16,因爲這是我將從中提取的表的大小。

public String CountryHandler2(int px, int ay) throws IOException{ 
    String url = "http://fx.sauder.ubc.ca/today.html"; 
    Document doc = Jsoup.connect(url).get(); 
    Elements paragraphs = doc.select("body > table:nth-child(4) > tbody:nth-child(1) > tr:nth-child("+px+") > td:nth-child("+ay+") > font:nth-child(1) > b:nth-child(1)"); 

    System.out.println("Paragraphs " + paragraphs.text()); 
    if(paragraphs.hasText()){ 
     return paragraphs.text(); 
    } 
    return null; 
} 
+1

請製作[mcve]。在這裏沒有辦法幫助你,因爲我們在問題本身沒有必要的信息。另見[問]。 – Tunaki

回答

0

我試過了,能夠提取表格數據。可能是你嘗試這種方式。

public static void main (String [] args) throws IOException{ 
     Document doc = Jsoup.connect("http://fx.sauder.ubc.ca/today.html").get(); 
     Element table = doc.select("table").get(2); //get the 3rd table 
     Elements trs = table.select("tr"); //get each row of that table 
     for (Element e: trs){ 
      System.out.println(e.text()); 
     } 
    }