2014-07-21 66 views
0

我需要從HTML頁面提取值。如何使用JAVA解析來自HTML頁面的值

該頁面包含此:

enter image description here

我想從那裏只提取值。

我試過這段代碼:

import java.io.*; 
import java.net.*; 
import javax.swing.text.html.*; 
import javax.swing.text.html.parser.*; 

public class Test extends HTMLEditorKit.ParserCallback { 
    StringBuffer txt; 
    Reader reader; 

    // empty default constructor 
    public Test() {} 

    // more convienient constructor 
    public Test(Reader r) { 
    setReader(r); 
    } 

    public void setReader(Reader r) { reader = r; } 

    public void parse() throws IOException { 
    txt = new StringBuffer(); 
    ParserDelegator parserDelegator = new ParserDelegator(); 
    parserDelegator.parse(reader, this, true); 
    } 

    public void handleText(char[] text, int pos) { 
    txt.append(text); 
    } 

    public String toString() { 
    return txt.toString(); 
    } 

    public static void main (String[] argv) { 
    try { 
     // the HTML to convert 
     URL toRead; 
     if(argv.length==1) 
     toRead = new URL(argv[0]); 
     else 
     toRead = new URL("http://test.com/values.html"); 

     BufferedReader in = new BufferedReader(
     new InputStreamReader(toRead.openStream())); 
     Test d = new Test(in); 
     d.parse(); 
     in.close(); 
     System.out.println(d.toString()); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 

而我得到的是這種提取物:

Measured valuestable{font-family:verdana,arial,helvetica,sans-serif;color:#000;font-size:10px;background-color:#fff;}Temperature:24.9°CRelative humidity:48.3%RHDew point:13.3°C 

是否有任何機會,只提取值?

25.0 
51.0 
14.1 

謝謝大家的幫助和理解。

真誠的問候。


謝謝大家的幫助。 如所建議的我用JSoup如下:

Document doc; 
    try { 

    // need http protocol 
    doc = Jsoup.connect("http:/test.com/values.html").get(); 



    String text = doc.text(); 

    System.out.println("text : " + text); 
      Element pending = doc.select("table td:eq(1)").get(0); 
      Element nextDate = doc.select("table td:eq(1)").get(1); 
      Element date = doc.select("table td:eq(1)").last(); 

      System.out.println(pending.text() + "\n" + nextDate.text() + "\n" + date.text()); 




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

}

其結果是這樣的:

23.9°C 
52.8%RH 
13.7°C 

不可能僅提取的值,而不ºC和%RH ?

對於給您帶來的不便,我們深表歉意。

+3

您可以使用JSoup,解析頁面並從特定標記中提取數據 –

+0

非常感謝您的回覆。你能給我一些示例代碼嗎? – rpirez

回答

1

嘿在使用我的jsoup的想法之後,你需要的是將字符串轉換爲帶有小數的數字,所以使用下面的代碼來獲得下面的結果。因爲元素不知道數字...

public static void main(String[] args) { 
    String str="23.9°C"; 
    System.out.println(str.replaceAll("[^0-9.]+", " ").toString()); 
    str="52.8%RH"; 
    System.out.println(str.replaceAll("[^0-9.]+", " ").toString()); 
    str="13.7°C"; 
    System.out.println(str.replaceAll("[^0-9.]+", " ").toString()); 
} 

23.9 
52.8 
13.7 
+0

rpirez,它解決了你的問題還是你需要任何其他的東西? – Harry

+0

非常感謝您的回覆,並感謝您的幫助。 – rpirez

+0

歡迎rpirez ... – Harry

1

rpirez,

使用Jsoup庫使用Java,它提供了文檔,元素,標籤,一行行解析HTML頁面的最佳途徑等等,

解析HTML頁面

示例: Document doc = Jsoup.connect(「http://en.wikipedia.org/」).get();

或得到由ID的元素,

//如果它是一個單一的數據

Document doc = Jsoup.parse(html); 

Element data1 = doc.getElementById("data1"); 

// If its a multiple data, 
Elements inputElements = data1.getElementsByTag("input"); 
// Using elements do something like this to parse the data perfectly,  
for (Element inputElement : inputElements) { 
    String key = inputElement.attr("name"); 
    String value = inputElement.attr("value"); 
} 

如果您在使用這個罐子的任何概率,請不要讓我們知道...

感謝和問候, 哈利

+0

感謝您的回覆,這真的很有用。我編輯我的問題,是否有可能幫助我僅提取值? – rpirez

+0

public static void main(String [] args){ \t \t String str =「23.9°C」; System.out.println(str.replaceAll(「[^ 0-9。] +」,「」).toString()); \t \t str =「52.8%RH」; System.out.println(str.replaceAll(「[^ 0-9。] +」,「」).toString()); \t \t str =「13.7℃」; System.out.println(str.replaceAll(「[^ 0-9。] +」,「」).toString()); \t} – Harry

+0

我的上面的代碼將爲你的轉換工作,你需要做的是將最終的pending.text()轉換爲字符串,然後使用我的上面的代碼,這將返回以下答案。 23.9 52.8 13.7 – Harry

0

谷歌的jericho,這是一個非常好的框架解析html頁面,這是比從apache Httpclient