`
ice123456
  • 浏览: 26917 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

抓出问天网的城市天气

阅读更多
package com.ice.weather.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.net.URL;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.text.SimpleDateFormat;

import com.ice.weather.bean.Weather;

public class OpenUrl {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws Exception{
		OpenUrl openUrl = new OpenUrl();
		List<Weather> list = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city=武汉");
		for (Weather w : list) {
			System.out.println("时间:" + w.getDate().toLocaleString());
			System.out.println("天气信息:" + w.getWeatherInfo());
			System.out.println("温度:" + w.getTemperature());
			System.out.println("风速信息:" + w.getWindInfo());
		}
	}
	
	/**
	 * 根据连接抓出5天天气
	 */
	public List<Weather> getWeatherByUrl(String url){
		try {
			String content = getContent(url);
			List<Weather> list = getWeathers(content);
			list.addAll(getWeathersBack(content));
			return list;
		} catch (Exception e) {
			System.out.println(e);
			return new ArrayList<Weather>();
		}
	}

	/**
	 * 根据连接地址抓出页面内容
	 * @param 根据一个连接地址
	 * @return 页面内容
	 */
	private String getContent(String strUrl){
		try {
			URL url = new URL(strUrl);
			BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
			String s = "";
			StringBuffer sb=new StringBuffer();
			while((s = br.readLine())!=null)
				sb.append(s+"\r\n");
			br.close();
			return sb.toString();
		} catch(Exception e) {
			return "error open url" + strUrl;
		}
	}

	/**
	 * 取当前3天内的天气情况
	 * content 为页面内容
	 */
	private List<Weather> getWeathers(String content) throws Exception {
		List<Weather> weatherList = new ArrayList<Weather>();
		int o = 6;
		String[] s = analysis("<td width=\"215\" align=\"center\" valign=\"middle\"><span class=\"big-cn\">(.*?)</span></td>", content , o);
		Weather weather;
		for (int i = 0; i < o; i += 2) {
			weather = new Weather();
			weather.setDate(conversionDate(s[i]));
			weather.setTemperature(s[i + 1]);
			weatherList.add(weather);
		}
		s = analysis("align=\"center\" valign=\"top\"><img src=\"../images/a(.*?).gif\" width=\"70\" height=\"65\"></td>" , content , o);
		s = ConversionWeather(s);
		o = 3;
		for (int i = 0; i < o; i++) 
			weatherList.get(i).setWeatherInfo(s[i]);
		s = analysis("<td width=\"215\" align=\"center\" valign=\"middle\"><span class=\"cn\">(.*?)</span></td>", content , o);
		for (int i = 0; i < o; i++)
			weatherList.get(i).setWindInfo(s[i]);
		return weatherList;
	}

	private List<Weather> getWeathersBack(String content) throws Exception {
		List<Weather> weatherList = new ArrayList<Weather>();
		int o = 2;
		String[] s = analysis("<td width=\"121\"><span class=\"cn\">(.*?)</span></td>" , content , o);
		Weather weather;
		for (int i = 0; i < o; i++) {
			weather = new Weather();
			weather.setDate(conversionDate(s[i]));
			weatherList.add(weather);
		}
		
		s = analysis("<td width=\"86\" class=\"cn\"><span class=\"wendu\">(.*?)</span></td>" , content , o);
		for (int i = 0; i < o; i++)
			weatherList.get(i).setTemperature(s[i]);
		
		s = analysis("<td width=\"157\"><span class=\"cn\">(.*?)</span></td>" , content , o);
		for (int i = 0; i < o; i++)
			weatherList.get(i).setWindInfo(s[i]);
	
		s = analysis("<img src=\"../images/b(.*?).gif\" width=\"50\" height=\"46\"></td>" , content , o*2);
		s = ConversionWeather(s);
		for (int i = 0; i < o; i++)
			weatherList.get(i).setWeatherInfo(s[i]);
		
		return weatherList;
	}

	/**
	 * 根据爬出到日期转换当前时间
	 */
	private Date conversionDate(String date) throws Exception {
		SimpleDateFormat dateformat = new SimpleDateFormat("MM月dd日");
		Date d = dateformat.parse(date);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
		return c.getTime();
	}

	/**
	 * 根据页面内容和正则表达式来分析页面,得到分析结果
	 * @param pattern 正则表达式
	 * @param match 页面内容
	 * @return content 结果
	 */
	private String[] analysis(String pattern, String match , int i) {
		Pattern sp = Pattern.compile(pattern);
	    Matcher matcher = sp.matcher(match);
	    String[] content = new String[i];
	    for (int i1 = 0; matcher.find(); i1++)
	    	content[i1] = matcher.group(1).trim();
	    return content;
	}

	/**
	 * 转换天气情况
	 * @param s 图片信息
	 * @return 天气情况
	 */
	private String[] ConversionWeather(String s[]) {
		String[] s1 = {"晴天", "多云", "阴", "阵雨", "雷阵雨", "雷阵雨并伴有冰雹", "雨加雪", "小雨", "中雨", "大雨", "暴雨", "大暴雨", "特大暴雨", "阵雪", "小雪", "中雪", "大雪", "暴雪", "雾", "冻雨", "沙尘暴", "小雨-中雨", "中雨-大雨", "大雨-暴雨", "暴雨-大暴雨", "大暴雨-特大暴雨", "小雪-中雪", "中雪-大雪", "大雪-暴雪", "浮尘", "扬沙", "强沙尘暴"};
		String[] s2 = new String[s.length/2];
		int i1 = 0;
		for (int i = 0; i < s.length; i += 2){
			if (s[i].trim().equals(s[i+1].trim()))
				s2[i1] = s1[Integer.parseInt(s[i])];
			else
				s2[i1] = s1[Integer.parseInt(s[i])] + "转" + s1[Integer.parseInt(s[i+1])];
			i1++;
		}
		return s2;
	}
}


package com.ice.weather.bean;

import java.util.Date;

public class Weather {
	private Date date;

	private String temperature;

	private String weatherInfo;

	private String windInfo;

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getTemperature() {
		return temperature;
	}

	public void setTemperature(String temperature) {
		this.temperature = temperature;
	}

	public String getWeatherInfo() {
		return weatherInfo;
	}

	public void setWeatherInfo(String weatherInfo) {
		this.weatherInfo = weatherInfo;
	}

	public String getWindInfo() {
		return windInfo;
	}

	public void setWindInfo(String windInfo) {
		this.windInfo = windInfo;
	}
}

最新修改过了··代码跟简洁...下午应该做一个js版的
分享到:
评论
2 楼 Michael.zhl 2007-10-17  
这样获得html是不是不太灵活?比如对方网站要求登陆,验证cookie
1 楼 ice123456 2007-10-16  
如果有意见  请提出

相关推荐

Global site tag (gtag.js) - Google Analytics