HTTPS HTTPClient

最近项目中需要增加天气预报功能,网上给的资料有很多缺陷比如

1.       有些小网站提供的webservers本身就不稳定不能长期使用。

2.       还有一些网站限制访问次数。

3.       再有就是花钱买服务。

 

根据以上几点只能放弃使用第三方的webservers,由此我们只能找个信得过的提供天气预报的网站抓取页面信息。

接下来我们以中央气象台为例实现从请求到数据解析的过程。

 

1.       浏览http://www.nmc.gov.cn/ 发现一个搜索功能

[原创]java 获得天气预报信息 - linfeng_0212 - 翱翔

做过网页的人都知道通过表单提交城市名字,那我们去看看具体提交到什么地方呢。

[原创]java 获得天气预报信息 - linfeng_0212 - 翱翔

在这个from表单中我们发现了什么?对有action 和 keys 这都知道是什么意思我就不废话了。

最终我们得到一个url : http://www.nmc.gov.cn/search_result.php?keys =城市名字

哈哈,这就是我们获得天气预报的关键。

 

2.       通过程序访问url.

以下是使用HttpClient的例子

HttpClient client = new HttpClient();//实例

String url="http://www.nmc.gov.cn//search_result.php?keys="+city;

method =  new GetMethod(url);//通过Get方式访问

httpstatus = client.executeMethod(method);

if(httpstatus==200)//200表示成功

{

//获得响应转码为GBK,否则中文会乱码

String  result=newString

(method.getResponseBodyAsString().getBytes("iso-8859-1"),"GBK");

}

 

用上面的程序访问你会发现报错了 Invalid query  ;这里我们需要将城市名字转码

 

String url="http://www.nmc.gov.cn//search_result.php?

keys="+URLEncoder.encode(city,”GBK”);

 

这样在url中中文就是转码后的不会出现乱码。

 

3.       解析数据

以北京为例提交后网页信息为:

 

[原创]java 获得天气预报信息 - linfeng_0212 - 翱翔

对应的页面中的代码为:

[原创]java 获得天气预报信息 - linfeng_0212 - 翱翔  

至此我们就可以获得天气信息了。。。。解析的代码如下:

 

        public  void ParaseHtml(String html)

    {

 

       String climate = ParaseWeatherInfo(html,"天气");

       String temperature = ParaseWeatherInfo(html,"气温");

       String wind = ParaseWeatherInfo(html,"风");

       String barometric = ParaseWeatherInfo(html,"气压");

 

       log .debug("天气:"+climate +"/ 气温 :"+temperature +"/ 风 "+wind +"/ 气压"+barometric);

    }

    public  String ParaseWeatherInfo(String str,String word)

    {

       String w = null;

       String st = "w365line1";

       //System.err.println(str);

       int a = str.indexOf(st);

       if(a!= -1)

       {

           String div1 = "<div";

           String div2 = "</div>";

           String keyword = ":";

           int d1 = str.lastIndexOf(div1, a);

           int d2 = str.indexOf(div2, a);

           if(d2 != -1)

           {

              String str4 = null;

              String str2 = str.substring(d1, d2+div2.length());

              if(str2.indexOf(word) !=-1)

              {

                  str4 = filerStr(str2,word,"");

                  int k1 = str4.indexOf(keyword);

                  str4 = str4.substring(k1+1,str4.length()).trim();

                  return str4;

              }  

              else

              {

                  String s5 = str.replace(str2, "");

                  w = ParaseWeatherInfo(s5,word);

                  return w;

              }

           }

       }

 

       return w;

    }

    public  String filerStr(String html,String word,String replacement)

    {   

    try{

        if(replacement==null)

            replacement = "";

        String   str=html; 

            String   str1="(<[^>]*>)|(&nbsp;)"; 

            Pattern   p=Pattern.compile (str1); 

            Matcher   m=p.matcher(str); 

            boolean   f=m.find(); 

            StringBuffer   sb=new   StringBuffer();  

            while(f)   { 

 

                m.appendReplacement(sb,replacement); 

                f=m.find(); 

            } 

            m.appendTail(sb);

 

            return sb.toString();

 

    }

    catch(Exception e)

    {

 

    }

 

    return html;

    }

 

抓取北京的天气信息:

       String html = WeatherUtil2.getInstance ().getNMCRespones("北京");

       WeatherUtil2.getInstance ().ParaseHtml(html);

 

天气:多云/ 气温 :30 ℃/ 风 西南风,3级,4米/秒/ 气压1001.8hPa

 

到这里关于天气预报的程序基本讲解完毕,欢迎大家指正。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值