如何从网页上获取城市天气信息?

编写一段Java代码,从如下网站http://www.weather.com.cn/ 获得西安的天气信息:
 
 
Java代码   收藏代码
  1. package com.url;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.net.URL;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9. import java.util.regex.Matcher;  
  10. import java.util.regex.Pattern;  
  11.   
  12. public class Weather {  
  13.   
  14.     public static void main(String[] args) {  
  15.   
  16.         String weatherReturn = getWeatherInfo();  
  17.   
  18.         //获得层div  
  19.         Pattern pattern = Pattern  
  20.                 .compile("<div class=\"weatherYubaoBox\">.*?</div>");  
  21.   
  22.         Matcher matcher = pattern.matcher(weatherReturn);  
  23.   
  24.         String weatherTable = "";  
  25.   
  26.         if (matcher.find()) {  
  27.             weatherTable = matcher.group();  
  28.         }  
  29.   
  30.         //找表  
  31.         String weatherToday = "";  
  32.           
  33.         if (!weatherTable.equals("") && weatherTable.length() > 0) {  
  34.               
  35.             pattern = Pattern  
  36.                     .compile("<table class=\"yuBaoTable\".*?>.*?</table>");  
  37.             matcher = pattern.matcher(weatherTable);  
  38.               
  39.             if (matcher.find()) {  
  40.                 weatherToday = matcher.group();  
  41.             }  
  42.         }  
  43.   
  44.         pattern = Pattern.compile("<a.*?</a>");  
  45.           
  46.         matcher = pattern.matcher(weatherToday);  
  47.           
  48.         List<String> weatherList = new ArrayList<String>();  
  49.         while (matcher.find()) {  
  50.               
  51.             weatherList.add(matcher.group());  
  52.             System.out.println(replaceTagA(matcher.group()));  
  53.               
  54.         }  
  55.     }  
  56.   
  57.     // 获取网页上的内容  
  58.     public static String getWeatherInfo() {  
  59.   
  60.         URL url = null;  
  61.         InputStreamReader inReader = null;  
  62.         BufferedReader reader = null;  
  63.           
  64.         try {  
  65.   
  66.             // 建立一个url  
  67.             url = new URL("http://www.weather.com.cn/weather/101110101.shtml");  
  68.   
  69.             // 打开一个流,并设置编码格式  
  70.             inReader = new InputStreamReader(url.openStream(), "utf-8");  
  71.             reader = new BufferedReader(inReader);  
  72.   
  73.             StringBuffer sb = new StringBuffer();  
  74.   
  75.             String inputLine;  
  76.   
  77.             while ((inputLine = reader.readLine()) != null) {  
  78.                 sb.append(inputLine);  
  79.             }  
  80.               
  81.             return sb.toString();  
  82.         } catch (Exception e) {  
  83.   
  84.             e.printStackTrace();  
  85.               
  86.         } finally {  
  87.             // 关闭流  
  88.             if (reader != null) {  
  89.                 try {  
  90.                     reader.close();  
  91.                 } catch (IOException e) {  
  92.                     e.printStackTrace();  
  93.                 }  
  94.             }  
  95.             if (inReader != null) {  
  96.                 try {  
  97.                     inReader.close();  
  98.                 } catch (IOException e) {  
  99.                     e.printStackTrace();  
  100.                 }  
  101.             }  
  102.         }  
  103.         return null;  
  104.     }  
  105.   
  106.     //从超连接中取数据  
  107.     public static String replaceTagA(String orginal) {  
  108.           
  109.         List<String> resultList = new ArrayList<String>();  
  110.           
  111.         if (orginal != null && !"".equals(orginal)) {  
  112.   
  113.             Pattern pattern = Pattern.compile(">.*?<");  
  114.             Matcher matcher = pattern.matcher(orginal);  
  115.             while (matcher.find()) {  
  116.                 String temp = matcher.group();  
  117.                 temp = temp.replace('>'' ');  
  118.                 temp = temp.replace('<'' ');  
  119.                 resultList.add(temp.trim());  
  120.             }  
  121.         }  
  122.         StringBuffer result = new StringBuffer();  
  123.         for (String temp : resultList) {  
  124.             result.append(temp);  
  125.         }  
  126.         return result.toString().trim();  
  127.     }  
  128. public class WeatherJob implements Job {     // 代表自动获取天气预报     private static final String AUTOMATIC = "0";          private IWeatherService weatherService;          public WeatherJob()     {         weatherService = (IWeatherService)PortalMSBeanFactory.getInstance()                 .getBean("portalWeatherService");     }          private static final DebugLogHelper logger = new DebugLogHelper(WeatherJob.class);     private  List<Weather> WeatherLists = new ArrayList<Weather>();          /**      * 执行定时任务      */     @SuppressWarnings("unchecked")     public void execute(JobExecutionContext context)     {         logger.enterFuncDebugLog();         String type = XMLFactory.getValueString("weather.type");         if (AUTOMATIC.equals(type))         {             logger.enterFuncDebugLog("======================Begin WeatherTaskManager.execute()======================");             List<WeatherImage> imageList = new ArrayList<WeatherImage>();                          DefaultHttpClient httpclient = new DefaultHttpClient();             String url = XMLFactory.getValueString("weather.weatherUrl");             HttpGet httpget = new HttpGet(url);             try             {                 logger.atFuncDebugLog();                 HttpResponse response = httpclient.execute(httpget);                                  HttpEntity entity = response.getEntity();                 if (entity != null)                 {                     WeatherInfo weather = new WeatherInfo();                                          InputStream in = entity.getContent();                                          int count = 0;                     while (count == 0)                     {                         count = Integer.parseInt("" + entity.getContentLength());                     }                     byte[] bytes = new byte[count];                                          // 已经成功读取的字节的个数                     int readCount = 0;                     while (readCount <= count)                     {                         if (readCount == count)                             break;                         readCount += in.read(bytes, readCount, count                                 - readCount);                     }                                          // 转换成字符串                     String readContent = new String(bytes, 0, readCount,                             "UTF-8");                                          Document document = DocumentHelper.parseText(readContent);                                          Element root = document.getRootElement();                                          // 获取根节点下的子节点active                     Element data = root.element("data");                                          // 天气信息                     String report = data.elementText("report");                                          String weatherInfo[] = report.split(";");                                          weather.setWeatherDesc(weatherInfo[0]);                     weather.setTemperature(weatherInfo[1].replace(ChineseCharacter.WEATHERJOB_TEMPERATURE,                             ""));                     weather.setType(0);                     weather.setUpdater(1L);                                          // 天气信息                     String dateStr = data.elementText("date");                     weather.setWeatherDate(dateStr);                                          List<?> fieldList = root.selectNodes("/root/data/weathericons/icon");                                          String suffix = Constants.SPLIT_DOT.getStringValue()                             + XMLFactory.getValueString("weather.imageSuffix");                     for (int i = 0; i < fieldList.size(); i++)                     {                         WeatherImage image = new WeatherImage();                         Element fieldEle = (Element)fieldList.get(i);                         image.setImage(fieldEle.getStringValue() + suffix);                         imageList.add(image);                     }                                          weather.setWeatherImageList(imageList);                                          try                     {                         weatherService.saveEntity(weather);                     }                     catch (PortalMSException e)                     {                         logger.excepFuncDebugLog("insert the weather is failed:", e);                     }                 }             }             catch (ClientProtocolException e1)             {                 logger.excepFuncDebugLog("query the weather is failed:", e1);             }             catch (IOException e1)             {                 logger.excepFuncDebugLog("query the weather is failed:", e1);             }             catch (Exception e1)             {                 logger.excepFuncDebugLog("query the weather is failed:", e1);             }                          logger.exitFuncDebugLog("======================WeatherTaskManager execute() Task End!======================");         }                     //                   WeatherLists = XMLFactory.getValueList("weathers.weather");                 List<WeatherInfo> WeatherList = new ArrayList<WeatherInfo>();                 try {                     WeatherList = weatherService.findWeatherConfig();                 } catch (PortalMSException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                 }                                  for (int j = 0; j < WeatherList.size(); j++)                 {                     WeatherInfo weatherinfo = new WeatherInfo();                     weatherinfo = WeatherList.get(j);                                          String weatherReturn = getWeatherInfo(weatherinfo.getWeatherUrl());                     // 获得table                     Pattern pattern = Pattern.compile("<table class=\"FcstBoxTable01\".*?>.*?</table>");                     Matcher matcher = pattern.matcher(weatherReturn);                     String weatherTable = "";                     if (matcher.find())                     {                         weatherTable = matcher.group();                     }                     // 找表                     String weatherToday = "";                     if (!weatherTable.equals("") && weatherTable.length() > 0)                     {                         pattern = Pattern.compile("<tbody>.*?</tbody>");                         matcher = pattern.matcher(weatherTable);                         if (matcher.find())                         {                             weatherToday = matcher.group();                         }                     }                     /**                                          * weather_id NUMBER(13) not null, city_code VARCHAR2(50), city_name                      * VARCHAR2(50) not null, weather_code VARCHAR2(50), weather_desc                      * VARCHAR2(100), temperature VARCHAR2(10), weather_date                      * VARCHAR2(20) not null, image VARCHAR2(512), update_date DATE,                      * type VARCHAR2(10)                      */                     // 获取今天的数据                     String str = weatherToday.substring(                             weatherToday.indexOf("<tr>", 1), weatherToday.indexOf(                                     "</tr>", 1));                     String temperature = str.substring(str.indexOf("<td>", 1) + 4, str                             .indexOf("</td>", 1))+"℃";                     // 天气情况                     pattern = Pattern.compile("alt=\".*?\"");                     matcher = pattern.matcher(str);                     String weather_desc = "";                     if (matcher.find())                     {                         weather_desc = matcher.group();                     }                     weather_desc = weather_desc.substring(5, weather_desc.indexOf("\"",5));                     // 天气预报图片                                          if(str.indexOf("day")>0)                     {                         pattern = Pattern.compile("day/.*?\"");                     }                     else pattern = Pattern.compile("night/.*?\"");                                          matcher = pattern.matcher(str);                     String weather_pic = "";                     if (matcher.find()) {                         weather_pic = matcher.group();                     }                     if(str.indexOf("day")>0)                     {                         weather_pic = weather_pic.substring(4, weather_pic.indexOf("\"", 4));                     }                     else                     {                         weather_pic = weather_pic.substring(6, weather_pic.indexOf("\"", 4));                     }                     weatherinfo.setTemperature(temperature);                     String day = DateUtil.fmtDate(new Date());                     weatherinfo.setWeatherDate(day);                     weatherinfo.setWeatherDesc(weather_desc);                     weatherinfo.setImageUrl(weather_pic);                     weatherinfo.setWeatherId(System.currentTimeMillis());                                                                //新增天气情况同步给portal                    syncWeatherInformation(Constants.SYNC_WEATHERINFO.getStringValue(), weatherinfo);                                                                                }                              }     //获取网页上的内容     public static String getWeatherInfo(String weatherUrl)     {              URL url = null;         InputStreamReader inReader = null;         BufferedReader reader = null;              try {                  // 建立一个url             url = new URL(weatherUrl);     //                "http://www.cwb.gov.tw/V7/forecast/taiwan/New_Taipei_City.htm");             // URL("http://www.weather.com.cn/weather/101110101.shtml");                  // 打开一个流,并设置编码格式             inReader = new InputStreamReader(url.openStream(), "utf-8");             reader = new BufferedReader(inReader);                  StringBuffer sb = new StringBuffer();                  String inputLine;                  while ((inputLine = reader.readLine()) != null)             {                 sb.append(inputLine);             }                  return sb.toString();         } catch (Exception e)         {                  e.printStackTrace();              } finally         {             // 关闭流             if (reader != null)             {                 try {                     reader.close();                 } catch (IOException e)                 {                     e.printStackTrace();                 }             }             if (inReader != null)             {                 try {                     inReader.close();                 } catch (IOException e)                 {                     e.printStackTrace();                 }             }         }         return null;     }     // 从超连接中取数据     public static String replaceTagA(String orginal) {              List<String> resultList = new ArrayList<String>();              if (orginal != null && !"".equals(orginal)) {                  Pattern pattern = Pattern.compile(">.*?<");             Matcher matcher = pattern.matcher(orginal);             while (matcher.find()) {                 String temp = matcher.group();                 temp = temp.replace('>', ' ');                 temp = temp.replace('<', ' ');                 resultList.add(temp.trim());             }         }         StringBuffer result = new StringBuffer();         for (String temp : resultList) {             result.append(temp);         }         return result.toString().trim();     }          public IWeatherService getWeatherService()     {         return weatherService;     }          public void setWeatherService(IWeatherService weatherService)     {         this.weatherService = weatherService;     }               public void syncWeatherInformation(String opCode, WeatherInfo weather)     {         logger.enterFuncDebugLog();         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         String updateDate = "";         if (weather.getWeatherDate() != null)         {             updateDate = weather.getWeatherDate();         }         IMessageSync messageSync = new MessageSync();         SyncWeatherInfoReqBody body = new SyncWeatherInfoReqBody();         WeatherInfoReqMsg weatherInfoReqMsg = new WeatherInfoReqMsg();         weatherInfoReqMsg.setCityCode(weather.getCityCode() == null ? "" : weather.getCityCode());         weatherInfoReqMsg.setCityName(weather.getCityName());         weatherInfoReqMsg.setImage(weather.getImageUrl()== null ? "" : weather.getImageUrl());         weatherInfoReqMsg.setTemperature(weather.getTemperature() == null ? "" : weather.getTemperature());         weatherInfoReqMsg.setUpdateDate(updateDate);         weatherInfoReqMsg.setWeatherID(String.valueOf(weather.getWeatherId()));         body.setWeatherInfo(weatherInfoReqMsg);         messageSync.syncMessage(body, opCode);         logger.exitFuncDebugLog();     } }

http://www.szmb.gov.cn/data_cache/szWeather/szweather2.1.xml

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值