Hunter的大杂烩 技术学习笔记

2007-05-25

html parser使用范例

Filed under: Java — hunter @ 4:03 pm

 

package org.htmlparser;

import org.htmlparser.beans.StringBean;
import java.util.regex.*;
import java.util.Formatter;
import java.util.Locale;
import java.lang.String;
import java.util.Calendar;

public class StockMonitor2
{

    public static void main (String[] args)
    {
        if (args.length < 3 )
        {
            System.out.println (“Syntax : java -classpath ../lib/htmlparser.jar id maxprice min”);
        }
        else
        {
            try
            {
                Integer stock_id = new Integer(args[0]);
                Float   stock_price = new Float(args[1]);
                Float   stock_price2 = new Float(args[2]);
               
                StringBean se;
                se = new StringBean();
                se.setLinks(false);
               
                //StringBuilder sb = new StringBuilder();
                //formatter.format(“%4$2s %3$2s %2$2s %1$2s”, “a”, “b”, “c”, “d”)

                Locale l = null;
                String sUrl = String.format(l, “http://www.aastocks.com/eng/StockQuote/default.asp?symbol=%04d”,stock_id.intValue());
                se.setURL(sUrl);
                //System.out.println(se.getStrings());
                Pattern pattern = Pattern.compile(“Bid[\r\n \t]+([0-9.]+)”);
                Matcher matcher = pattern.matcher(se.getStrings());
                //Calendar rightNow = Calendar.getInstance();

                //System.out.println(“monitor time: ” + rightNow.get(Calendar.HOUR_OF_DAY) + “:” +
                //                    rightNow.get(Calendar.MINUTE) + “:” +rightNow.get(Calendar.SECOND));
                if (matcher.find())
                {
                    StringBuffer sBuffer = new StringBuffer(matcher.group());
                    Float now_price = new Float(sBuffer.substring(4));
                    if (now_price.compareTo(stock_price)>0 || now_price.compareTo(stock_price2)<0)
                    {
                        System.out.println(“

“+stock_id+” “+stock_price+” “+stock_price2+” “+now_price+” “+stock_id+” “+stock_price+” “+stock_price2+” “+now_price+”

“);
                    }
                }
                else
                {
                    System.err.println(“can’t match data:\n” + se.getStrings());
                }
            }
            catch (NumberFormatException e)
            {
                e.printStackTrace();
            }
        }
    }
}

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress