编写一段Java代码,从如下网站http://www.weather.com.cn/ 获得西安的天气信息:
- package com.url;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Weather {
- public static void main(String[] args) {
- String weatherReturn = getWeatherInfo();
- //获得层div
- Pattern pattern = Pattern
- .compile("<div class=\"weatherYubaoBox\">.*?</div>");
- Matcher matcher = pattern.matcher(weatherReturn);
- String weatherTable = "";
- if (matcher.find()) {
- weatherTable = matcher.group();
- }
- //找表
- String weatherToday = "";
- if (!weatherTable.equals("") && weatherTable.length() > 0) {
- pattern = Pattern
- .compile("<table class=\"yuBaoTable\".*?>.*?</table>");
- matcher = pattern.matcher(weatherTable);
- if (matcher.find()) {
- weatherToday = matcher.group();
- }
- }
- pattern = Pattern.compile("<a.*?</a>");
- matcher = pattern.matcher(weatherToday);
- List<String> weatherList = new ArrayList<String>();
- while (matcher.find()) {
- weatherList.add(matcher.group());
- System.out.println(replaceTagA(matcher.group()));
- }
- }
- // 获取网页上的内容
- public static String getWeatherInfo() {
- URL url = null;
- InputStreamReader inReader = null;
- BufferedReader reader = null;
- try {
- // 建立一个url
- url = new 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 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
如何从网页上获取城市天气信息?
最新推荐文章于 2024-03-09 13:40:59 发布