2012-04-01 65 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