2014-07-21 60 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