通过HTML PARSER编程实时获取外汇牌价

关于HTMLPARSER的使用,可以参看
[url]http://blog.csdn.net/gogir/archive/2006/06/12/791716.aspx[/url]

[url]http://secure.efxnow.com/Rates2/CHN/Rates_CHN.aspx[/url]是一个提供外汇实时牌价的网站,现在我们就要要获取上面的外汇牌价
下面是一个获取欧元兑美元的卖价和买价的实例============================TestHtmlParser.java==================================

import org.htmlparser.filters.*;
import java.io.*;
import org.htmlparser.*;
import org.htmlparser.util.*;

public class TestHtmlParser{    
public static void main (String[] args) throws Exception{
                String eu = null;
    String tmp = null;
     float euBid = 0f;                                         //欧元兑美元的卖价
     float euOffer = 0f;                                 //欧元兑美元的买价
    
    Parser parser = new Parser( "http://secure.efxnow.com/Rates2/CHN/Rates_CHN.aspx");
    
    TagNameFilter filter = new TagNameFilter("TD");
    NodeList nl =parser.extractAllNodesThatMatch(filter);

    for(int i =1;i <nl.size();i++){
     tmp = nl.elementAt(i).toPlainTextString();
     if (tmp.indexOf("EUR/USD")>0){eu = nl.elementAt(i+1).toPlainTextString();}
    }
    eu=eu.replaceAll(" ","");
    eu=eu.replaceAll(" ","");

    euBid = Float.parseFloat(eu.substring(0,6));
    euOffer = Float.parseFloat(eu.substring(0,4)+eu.substring(7,9));
    System.out.println("EUR/USD Bid price is " + euBid);
    System.out.println("EUR/USD Offer price is " + euOffer);

}
}

怎么样,很简单吧,^o^
source: [url]http://www.yuanma.org/data/2006/0622/article_904.htm[/url]

你可能感兴趣的:(java,职场,HtmlParser,休闲)