天气小工具 <三>
之前我们能得到省份,市,区的唯一ID,则我们可以根据这个ID获得当前想要查看的地区的天气状况。
继续使用我们的HttpWatch分析。例如我们希望知道江苏省南京市南京市区的天气状况。通过之前的城市ID分析,我们知道江苏省ID为10119,南京市ID01,南京市区ID为01,则查询ID为101190101,天气查询URL为:http://www.weather.com.cn/weather/101190101.shtml
这个返回的信息很符合HTML的标准。
我们可以使用HTMLParser提取信息,当然也可以使用正则表达式。
// 制定得到天气的FilterNodeFilter prosFilter = new TagNameFilter("table");NodeFilter prosAttrFilter = new HasAttributeFilter("class","yuBaoTable");AndFilter filters = new AndFilter(prosFilter, prosAttrFilter);// 得到对应的天气NodeList nodes = parser.extractAllNodesThatMatch(filters);for (int i = 0; i < nodes.size(); i++) {Node node = nodes.elementAt(i);String weatherInfo = node.toPlainTextString();weatherInfo = weatherInfo.replaceAll("\\s", "");}public static Vector<Day> GetWeather(String content) {Vector<Day> days = new Vector<Day>();// 根据所获得的天气网页信息得到未来七天温度信息Parser parser;try {parser = new Parser(content);// 制定得到天气的FilterNodeFilter prosFilter = new TagNameFilter("table");NodeFilter prosAttrFilter = new HasAttributeFilter("class","yuBaoTable");AndFilter filters = new AndFilter(prosFilter, prosAttrFilter);// 得到对应的天气NodeList nodes = parser.extractAllNodesThatMatch(filters);for (int i = 0; i < nodes.size(); i++) {Node node = nodes.elementAt(i);String weatherInfo = node.toPlainTextString();weatherInfo = weatherInfo.replaceAll("\\s", "");//System.out.print(weatherInfo);// 得到的天气信息有两种,一种白天和夜间都有,一种只有白天if (weatherInfo.indexOf("夜") != -1&& weatherInfo.indexOf("白") != -1) {String day = "", week = "", h_weather = "", high = "", h_wind = "", h_wind_level = "", l_weather = "", low = "", l_wind = "", l_wind_level = "";if (weatherInfo.indexOf("风向") != -1) {Pattern adayPattern = Pattern.compile(MyRegex.WeatherRegexNoWind);Matcher adayMatcher = adayPattern.matcher(weatherInfo);while (adayMatcher.find()) {day = adayMatcher.group(1);week = adayMatcher.group(2);h_weather = adayMatcher.group(3);high = "高温" + adayMatcher.group(4) + "℃";h_wind = adayMatcher.group(5) + "风向"; // 白天的风向h_wind_level = adayMatcher.group(6) + "风"; // 风力l_weather = adayMatcher.group(7);low = "低温" + adayMatcher.group(8) + "℃";l_wind = adayMatcher.group(9) + "风向";l_wind_level = adayMatcher.group(10) + "风";}} else {Pattern adayPattern = Pattern.compile(MyRegex.WeatherRegex);Matcher adayMatcher = adayPattern.matcher(weatherInfo);while (adayMatcher.find()) {day = adayMatcher.group(1);week = adayMatcher.group(2);h_weather = adayMatcher.group(3);high = "高温" + adayMatcher.group(4) + "℃";h_wind = adayMatcher.group(5) + "风"; // 白天的风向h_wind_level = adayMatcher.group(6) + "级"; // 风力l_weather = adayMatcher.group(7);low = "低温" + adayMatcher.group(8) + "℃";l_wind = adayMatcher.group(9) + "风";l_wind_level = adayMatcher.group(10) + "级";}}Day aday = new Day(day, week, h_weather, high, h_wind,h_wind_level, l_weather, low, l_wind, l_wind_level);days.add(aday);//System.out.println(aday);} else if (weatherInfo.indexOf("白") != -1) {String day = "", week = "", h_weather = "", high = "", h_wind = "", h_wind_level = "";if (weatherInfo.indexOf("风向") != -1) {Pattern adayPattern = Pattern.compile(MyRegex.WeatherDayRegexNoWind);Matcher adayMatcher = adayPattern.matcher(weatherInfo);while (adayMatcher.find()) {day = adayMatcher.group(1);week = adayMatcher.group(2);h_weather = adayMatcher.group(3);high = "高温" + adayMatcher.group(4) + "℃";h_wind = adayMatcher.group(5) + "风向"; // 白天的风向h_wind_level = adayMatcher.group(6) + "风"; // 风力}} else {Pattern adayPattern = Pattern.compile(MyRegex.WeatherDayRegex);Matcher adayMatcher = adayPattern.matcher(weatherInfo);while (adayMatcher.find()) {day = adayMatcher.group(1);week = adayMatcher.group(2);h_weather = adayMatcher.group(3);high = "高温" + adayMatcher.group(4) + "℃";h_wind = adayMatcher.group(5) + "风"; // 白天的风向h_wind_level = adayMatcher.group(6) + "级"; // 风力}}Day aday = new Day(day, week, h_weather, high, h_wind,h_wind_level, "", "", "", "");days.add(aday);//System.out.println(aday);} else {String day = "", week = "", l_weather = "", low = "", l_wind = "", l_wind_level = "";if (weatherInfo.indexOf("风向") != -1) {Pattern adayPattern = Pattern.compile(MyRegex.WeatherNightRegexNoWind);Matcher adayMatcher = adayPattern.matcher(weatherInfo);while (adayMatcher.find()) {day = adayMatcher.group(1);week = adayMatcher.group(2);l_weather = adayMatcher.group(3);low = "低温" + adayMatcher.group(4) + "℃";l_wind = adayMatcher.group(5) + "风向";l_wind_level = adayMatcher.group(6) + "风";}} else {Pattern adayPattern = Pattern.compile(MyRegex.WeatherNightRegex);Matcher adayMatcher = adayPattern.matcher(weatherInfo);while (adayMatcher.find()) {day = adayMatcher.group(1);week = adayMatcher.group(2);l_weather = adayMatcher.group(3);low = "低温" + adayMatcher.group(4) + "℃";l_wind = adayMatcher.group(5) + "风";l_wind_level = adayMatcher.group(6) + "级";}Day aday = new Day(day, week, "", "", "", "",l_weather, low, l_wind, l_wind_level);days.add(aday);//System.out.println(aday);}}}} catch (ParserException e) {e.printStackTrace();}return days;}