利用正则表达式,提取天气预报。

因为这是本人申请实习时,出的一道程序题。实际上,本人对正则表达式本身并不熟悉,基本的语法感觉很细,也没有太多时间去学习。本文也就是从大的方向利用正则表达式来抽取天气预报信息。所用到的正则表达式也比较简单,容易理解。程序可能会有繁琐的地方,不过也不打紧,大家取精去糙,为其所用。如果能给大家带来一点帮助或是启发,本人已经受宠若惊。
程序清单如下:

import  java.io.BufferedReader;
import  java.io.BufferedWriter;
import  java.io.File;
import  java.io.FileWriter;
import  java.io.IOException;
import  java.io.InputStreamReader;
import  java.net.MalformedURLException;
import  java.net.URL;
import  java.net.URLConnection;
import  java.util.regex. * ;


public   class  WeatherForecastExtractor  {
private String outputPath = "C:/output.txt";

private static String getDocumnetAt(String urlString){
    StringBuffer html_text 
= new StringBuffer();
    
    
try{
        
//创建指向天气预报网址的链接
        URL url =  new URL(urlString);
        
//创建链接
        URLConnection conn = url.openConnection();
        
//创建输入流
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        
//将网页内容放到缓冲区
        String line = null;
        
while((line = reader.readLine()) != null){
            html_text.append(line 
+ " ");
        }

        
//关闭输入流
        reader.close();
    }
 catch (MalformedURLException e){
        System.out.print(
"invalid url:" + urlString );
    }
 catch (IOException e){
        e.printStackTrace();
    }

    
return html_text.toString();
}


public void extractor(String urlString) throws IOException{
    
//天气预报来源
    String source = null;
    
    
//天气预报条目
    String item = new String();
    
    
//天气预报数据
    String data = new String();
    
    
//文件输出流
    BufferedWriter bw = null;
    
    
try{
        bw 
= new BufferedWriter(new FileWriter(new File(outputPath)));
        
//获得网页文本内容
        String str = WeatherForecastExtractor.getDocumnetAt(urlString);
        
//创建提取天气预报来源的正则表达式
        Pattern pt_source = Pattern.compile("<font class="f14"><b>(.*)</b></font>(.*)<tr><td align=middle height=20 valign=top class='font'><div align='center'><font color='#2020ff'>(.*)<font color='#FF0000'>(.*)<font color='#FF0000'>", Pattern.MULTILINE | Pattern.DOTALL); 
        Matcher mc 
= pt_source.matcher(str);
        
while(mc.find()){
            
//提取天气预报来源
            source = mc.group(1).trim() + mc.group(2).replace("<br>""").trim() + " " +
                     mc.group(
3).trim().replace("<br>""")  + mc.group(4).trim().replace("<br>""");
            
//System.out.print(source);
            bw.write(source + " " + " ");
        }

   
        
//提取天气预报主体
        Pattern pt_body = Pattern.compile("<table  border='1' width='520' align='center' cellpadding='1' cellspacing='1'>(.*)</table></tr>",Pattern.MULTILINE|Pattern.DOTALL);
        mc 
= pt_body.matcher(str);
        String body 
= null;
        
while(mc.find()){
             body 
= mc.group(1).trim();
            
// System.out.println(body);
        }

        
        
//提取天气预报条目
        Pattern pt_item = Pattern.compile("<tr bgcolor='#DEF3FF'>(.*)</td></tr>", Pattern.MULTILINE | Pattern.DOTALL);
        mc 
= pt_item.matcher(body);
        String itemstr 
= null;
        
while(mc.find()){
            itemstr 
= mc.group(1).trim();
        }

        
        
//输出天气预报条目
        Pattern pt_item_out = Pattern.compile("<div align='center'>(.*)</div>");
        mc 
= pt_item_out.matcher(itemstr);
        String item1 
= new String();
        
while(mc.find()){//控制一下格式
             if(mc.group(1).contains("</div><div align='center'>"|| mc.group(1).contains("风向")){
                 item 
=  item + mc.group(1).replace("</div><div align='center'>",""+ " ";     
             }
else{
                 
if(mc.group(1).equals("时次")){
                     item1 
= item1 + mc.group(1).trim() + "                                          " ;
                 }
else {
                     item1 
= item1 + mc.group(1).trim() + "      ";
                 }

             }
     
        }

        bw.write(item1 
+ " ");
        bw.write(
"    " + item + " ");
        
        
//提取天气预报详细情况
        Pattern pt_data = Pattern.compile("<tr bgcolor='#EEF5E5'>(.*)</tr>", Pattern.MULTILINE | Pattern.DOTALL);
        mc 
= pt_data.matcher(body);
        String datastr 
= null;
        
while(mc.find()){
            datastr 
= mc.group(1).trim();
        }

        
        
//输出天气预报详细情况
        Pattern pt_data_out = Pattern.compile("<div align='center'>(.*)</div>");
        mc 
= pt_data_out.matcher(datastr);
        
int i = 0;
        
while(mc.find()){//控制一下格式
            data = " " + data + mc.group(1+ "     ";
            i
++;
            
if(i % 9 == 0){
            data 
= data + " ";
            }

        }
      
        bw.write(data 
+ " ");
        
    }
 catch (IOException e){
        e.printStackTrace();
    }
 catch (PatternSyntaxException e){
        System.out.println(
"Regular expression syntax error");    
    }
catch (IllegalStateException e){
        System.out.println(
"Do not find the pattern");    
    }
finally{
        
if(bw != null){
            bw.close();
        }

    }

}

    
public static void main(String[] args){
        WeatherForecastExtractor weatherforecastextractor 
= new WeatherForecastExtractor();
        
try{
            weatherforecastextractor.extractor(
"http://cdc.bjmb.gov.cn/gongzhong.asp");
        }
 catch(IOException e){
            e.printStackTrace();
        }

    }



}

下为输出:
北京市实时自动气象站资料(数据未经质量控制,仅供参考)
2008年4月16日 0:00至2008年4月16日 23:00 站名:南郊观象台

时次                                          2分钟平均风      极大风     
    气温(℃) 雨量(mm) 相对湿度(%) 气压(Hpa) 风速(m/s) 风向 风速(m/s) 风向
                                                                                                                                                                                                                        23     19.0     0.0     59     1009.5     1.2     东东北     2.5     东    
22     21.0     0.0     43     1009.7     1.7     东     2.8     东东南    
21     21.0     0.0     44     1009.5     1.0     南     1.6     南西南    
20     23.0     0.0     35     1008.4     1.3     南西南     4.4     南西南    
19     24.0     0.0     36     1008.0     1.9     南西南     2.6     南西南    
18     25.0     0.0     26     1007.8     1.4     西西南     6.6     南西南    
17     26.0     0.0     24     1007.5     3.9     南西南     6.0     西南    
16     26.0     0.0     24     1007.1     4.4     西南     8.6     西南    
15     26.0     0.0     28     1007.4     5.9     南西南     7.9     南西南    
14     27.0     0.0     23     1008.1     4.6     南西南     7.7     南西南    
13     26.0     0.0     30     1009.0     3.7     南     5.3     南西南    
12     24.0     0.0     34     1009.8     3.0     南东南     4.5     南    
11     22.0     0.0     55     1010.7     1.5     东     3.0     东    
10     20.0     0.0     60     1011.1     0.8     东     2.9     东北    
09     18.0     0.0     67     1010.9     1.2     北东北     2.0     北东北    
08     16.0     0.0     76     1010.9     1.0     北东北     2.0     东东北    
07     14.0     0.0     82     1010.4     1.3     南东南     2.0     东北    
06     13.0     0.0     84     1009.7     1.4     东     2.1     北东北    
05     14.0     0.0     82     1008.9     1.2     北东北     2.1     北东北    
04     14.0     0.0     79     1008.7     1.4     东东北     1.9     东东北    
03     15.0     0.0     73     1008.9     1.4     北东北     1.9     北东北    
02     17.0     0.0     66     1008.7     1.2     东     2.8     南西南    
01     18.0     0.0     64     1008.8     1.3     东南     3.3     东南    
00     18.0     0.0     63     1008.7     1.6     南     2.5     南东南    



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值