隨筆-65  評論-68  文章-4  trackbacks-0

          ///////////////////////////////////////////////////
          //其實這個程序可以進一步處理進而過濾出需要的信息
          //@author Xiaoshuang
          //

          import java.sql.*;
          import java.net.*;
          import java.io.*;

          public class WeatherFilter{
           private String html;
           private String target="
          http://weather.news.sohu.com/query.php?city=北京";
           
           public WeatherFilter()throws Exception{
            this(null);
           }
           
           public WeatherFilter(String targetIn)throws Exception{
            if(targetIn!=null)
             this.target=targetIn;
            URL targetURL=new URL(target);
            HttpURLConnection conn=(HttpURLConnection)targetURL.openConnection();
            conn.connect();
            InputStream inputSteam=conn.getInputStream();
            BufferedReader bufReader=new BufferedReader(new InputStreamReader(inputSteam));
            StringBuffer buf=new StringBuffer();
            String temp;
            while((temp=bufReader.readLine())!=null){
             buf.append(temp);
            }
            html=buf.toString();
            html=html.toLowerCase();
           }
           
           public String toString(){
            return html;
           }
           
           private void disposeSpaces(){
            html=html.replaceAll(" ","");
            html=html.replaceAll("\t","");
           }
           
           private void replaceAllInTag(String tag,String replace){
            while(replaceAStyle(tag,replace));
           }
           
           private boolean replaceAStyle(String tag,String replace){
            int pageLength=html.length();
            int loc = html.indexOf("<"+tag);
            if(loc>=0){
             int loc2=html.indexOf("</"+tag+">")+8;
             StringBuffer buf=new StringBuffer();
             for(int i=0;i<loc;i++)
              buf.append(html.charAt(i));
             if(replace!=null)
              buf.append(replace);
             for(int i=loc2;i<pageLength;i++)
              buf.append(html.charAt(i));
             html=buf.toString();
             return true;
            }
            else{
             return false;
            }
           }
           
           private void markAllImages(){
            boolean dispose=false;
            do{
             StringBuffer buf=new StringBuffer();
             int pageLength=html.length();
             int end=html.indexOf("<img");
             for(int i=0;i<end;i++)
              buf.append(html.charAt(i));
             buf.append("\n");
             while(html.charAt(++end)!='>');
             for(int i=end+1;i<pageLength;i++){
              buf.append(html.charAt(i));
             }
             html=buf.toString();
             buf=null;
             System.gc();
            }while(html.indexOf("<img")>=0);
           }

           
           private void disposeAllTags(){
            int pageLength=html.length();
            StringBuffer buf=new StringBuffer();
            boolean dispose=false;
            for(int i=0;i<pageLength;i++){
             if(html.charAt(i)=='<'){
              dispose=true;
              continue;
             }
             if(html.charAt(i)=='>'){
              dispose=false;
              continue;
             }
             if(!dispose){
              buf.append(html.charAt(i));
             }
            }
            html=buf.toString();
           }
           
           private void disposeAllKeyChars(){
            int pageLength=html.length();
            StringBuffer buf=new StringBuffer();
            boolean dispose=false;
            for(int i=0;i<pageLength;i++){
             if(html.charAt(i)=='&'){
              dispose=true;
              continue;
             }
             if(html.charAt(i)==';'){
              dispose=false;
              buf.append('\n');
              continue;
             }
             if(!dispose){
              buf.append(html.charAt(i));
             }
            }
            html=buf.toString();
           }

           
           public static void main(String[] args)throws Exception{
            WeatherFilter filter;
            if(args.length==1)
             filter=new WeatherFilter(args[args.length-1]);
            else
             filter=new WeatherFilter();
            filter.replaceAllInTag("style",null);
            filter.replaceAllInTag("script",null);
            filter.markAllImages();
            filter.disposeAllTags();
            filter.disposeSpaces();
            filter.disposeAllKeyChars();
            System.err.print(filter);
           }
          }

          posted on 2005-09-15 21:18 小爽 閱讀(1213) 評論(0)  編輯  收藏 所屬分類: 我的Java
          主站蜘蛛池模板: 龙海市| 旬阳县| 嘉义市| 洪泽县| 万州区| 洛扎县| 郎溪县| 永城市| 石渠县| 利川市| 荔浦县| 西昌市| 周口市| 东阿县| 沈阳市| 大石桥市| 买车| 乌鲁木齐市| 遵化市| 成都市| 芦山县| 佳木斯市| 漾濞| 津市市| 德令哈市| 沂南县| 富蕴县| 兴海县| 绩溪县| 揭阳市| 巴里| 阿荣旗| 罗江县| 札达县| 临潭县| 丰镇市| 平山县| 游戏| 屏边| 砚山县| 花垣县|