2012-04-01 52 views
0

我期待解析從這個網頁實時價格網頁:http://www.truefx.com/到我的Java程序,即,我想從由第二基礎上,第二刷新網頁中的數據進行不斷流進入我的程序。解析用java

我想這樣做,如果可能,使用標準的java庫。我知道像jsoup可能還有其他的插件,但我想沒有下載和安裝的插件,因爲我使用的硬盤驅動器是基於在加利福尼亞州和一切,但一些核心課程的計算機,日食是上當系統重新啓動時,每晚都會被刪除。

所以,如果有人知道在標準的Eclipse下載,可以做到這一點,請讓我知道!謝謝


好吧,所以我得到了這個工作,但它似乎很慢。例如,數據將在第二個基礎上發生變化,即使我正在刷新從第二個基礎讀取的網頁(我使用thread.sleep(1000)),然後獲取新實例的網頁,每分鐘只更新一次。是什麼賦予了?

這裏是我的代碼看起來像(我用你上面貼東西作爲我的網址讀者):

public String getPage(String urlString){ 
     String result = ""; 
     //Access the page 
     try { 
     // Create a URL for the desired page 
     URL url = new URL(urlString); 
     // Read all the text returned by the server 
     BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
     String str; 
     while ((str = in.readLine()) != null) { 
      // str is one line of text; readLine() strips the newline character(s) 
      result += str; 
     } 
     in.close();    
     } catch (MalformedURLException e) { 
     } catch (IOException e) { 
     }   
     return result; 
    } 

    public static void main(String[]args){ 
     int i =0; 
     Reading r = new Reading(); 

    while(true){ 
     try{Thread.sleep(1000);}catch(Exception e){} 
     String page = new String(r.getPage("http://www.fxstreet.com/rates-charts/forex-rates/")); 
     int index = page.indexOf("last_3212166"); 
     //System.out.println(i+page); 
     i++; 
     System.out.println(i+"GBP/USD: "+page.substring(index+14,index+20)); 
    } 
+0

標準的Java庫沒有任何HTML解析功能。另外,你有沒有看過這個呢?看起來像一個快速谷歌可能已經扭轉了局面。另外,如果每件事都被刪除,你怎麼保持你的源代碼?編輯:其實,它看起來像Java確實有一些內置的東西:http://en.wikipedia.org/wiki/Java_API_for_XML_Processing – Corbin 2012-04-01 02:29:29

+0

感謝。我仔細研究了一下,但在詢問之前可能會稍微查找一下。 – russell88 2012-04-01 02:36:26

+0

以及它不是真的那些東西被刪除,但更多的是與類路徑被重置或什麼。我不完全理解系統是如何建立的,我只知道你不能永久地改變班級路徑。 – russell88 2012-04-01 02:37:54

回答

1

隨着沒有外部API您可以通過此功能讓頁面只是進口java.net.URL

static public String getPage(String urlString){ 
    String result = ""; 
    //Access the page 
    try { 
    // Create a URL for the desired page 
    URL url = new URL(urlString); 
    // Read all the text returned by the server 
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
    String str; 
    while ((str = in.readLine()) != null) { 
     // str is one line of text; readLine() strips the newline character(s) 
     result += str; 
    } 
    in.close();    
    } catch (MalformedURLException e) { 
    } catch (IOException e) { 
    }   
    return result; 
} 

然後使用java.util.regex以匹配你想從日獲取數據e頁。並將其解析爲標籤。不要忘記把所有這些放在一個線程while(true)循環,和一個睡眠(some_time)有第二個信息。

+0

真棒,正是我想要的!非常感謝。 – russell88 2012-04-01 20:08:31