如何从网页上获取城市天气信息?
编写一段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();//获得层divPattern 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 {// 建立一个urlurl = 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();}}??
????