2007-10-16

抓出问天网的城市天气

关键字: 源码
package com.zdao.weather.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zdao.weather.bean.Weather;

public class OpenUrl {
	public static void main(String[] args){
		OpenUrl openUrl = new OpenUrl();
		Weather w = openUrl.getWeatherByUrl("http://weather.tq121.com.cn/detail.php?city=武汉");
		System.out.println(w.getDate1());
		System.out.println(w.getTemperature1());
		System.out.println(w.getWeather1());
		System.out.println(w.getWind1());
		
		System.out.println(w.getDate2());
		System.out.println(w.getTemperature2());
		System.out.println(w.getWeather2());
		System.out.println(w.getWind2());
		
		System.out.println(w.getDate3());
		System.out.println(w.getTemperature3());
		System.out.println(w.getWeather3());
		System.out.println(w.getWind3());
		
		System.out.println(w.getDate4());
		System.out.println(w.getTemperature4());
		System.out.println(w.getWeather4());
		System.out.println(w.getWind4());
		
		System.out.println(w.getDate5());
		System.out.println(w.getTemperature5());
		System.out.println(w.getWeather5());
		System.out.println(w.getWind5());
	}
	
	/**
	 * 根据连接抓出5天天气
	 */
	public Weather getWeatherByUrl(String url){
		Weather weather = new Weather();
		try {
			long l1 = System.currentTimeMillis();

			String content = getContent(url);
			weather = getDateTemperature(content , weather);
			weather = getWind3(content , weather);
			weather = getWeather3(content , weather);
			weather = getDate(content , weather);
			weather = getTemperature(content , weather);
			weather = getWind2(content , weather);
			weather = getWeather2(content , weather);
			long l2 = System.currentTimeMillis();
			System.out.println(l2 - l1);
		}catch (Exception e){
			System.out.println(e);
		}
		return 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天内的温度和时间
	 * 返回一个天气bean
	 * 0和1 2个参数为第一天时间和温度 后面相同
	 * content 为页面内容
	 * weather 为天气bean
	 */
	private Weather getDateTemperature(String content , Weather weather) throws Exception{
		String[] s = analysis("<td width=\"215\" align=\"center\" valign=\"middle\"><span class=\"big-cn\">(.*?)</span></td>", content , 6);
		weather.setDate1(s[0].trim());
		weather.setTemperature1(s[1].trim());
		weather.setDate2(s[2].trim());
		weather.setTemperature2(s[3].trim());
		weather.setDate3(s[4].trim());
		weather.setTemperature3(s[5].trim());
		return weather;
	}

	/**
	 * 取前3天的风向
	 * content 为页面内容
	 * weather 为天气bean
	 */
	private Weather getWind3(String content , Weather weather){
		String[] s = analysis("<td width=\"215\" align=\"center\" valign=\"middle\"><span class=\"cn\">(.*?)</span></td>", content , 3);
		weather.setWind1(s[0].trim());
		weather.setWind2(s[1].trim());
		weather.setWind3(s[2].trim());
		return weather;
	}
	
	/**
	 * 取前3天的天气情况
	 * content 为页面内容
	 * weather 为天气bean
	 */
	private Weather getWeather3(String content , Weather weather){
		String[] s = analysis("align=\"center\" valign=\"top\"><img src=\"../images/a(.*?).gif\" width=\"70\" height=\"65\"></td>" , content , 6);
		s = ConversionWeather(s);
		weather.setWeather1(s[0]);
		weather.setWeather2(s[1]);
		weather.setWeather3(s[2]);
		return weather;
	}
	
	/**
	 * 取后2天的日期
	 */
	private Weather getDate(String content , Weather weather){
		String[] s = analysis("<td width=\"121\"><span class=\"cn\">(.*?)</span></td>" , content , 2);
		weather.setDate4(s[0].trim());
		weather.setDate5(s[1].trim());
		return weather;
	}
	
	/**
	 * 取后天2天的温度
	 */
	private Weather getTemperature(String content , Weather weather){
		String[] s = analysis("<td width=\"86\" class=\"cn\"><span class=\"wendu\">(.*?)</span></td>" , content , 2);
		weather.setTemperature4(s[0].trim());
		weather.setTemperature5(s[1].trim());
		return weather;
	}
	
	/**
	 * 取后天2天的风向
	 */
	private Weather getWind2(String content , Weather weather){
		String[] s = analysis("<td width=\"157\"><span class=\"cn\">(.*?)</span></td>" , content , 2);
		weather.setWind4(s[0].trim());
		weather.setWind5(s[1].trim());
		return weather;
	}
	
	/**
	 * 
	 */
	private Weather getWeather2(String content , Weather weather){
		String[] s = analysis("<img src=\"../images/b(.*?).gif\" width=\"50\" height=\"46\"></td>" , content , 4);
		s = ConversionWeather(s);
		weather.setWeather4(s[0]);
		weather.setWeather5(s[1]);
		return weather;
	}

	/**
	 * 根据页面内容和正则表达式来分析页面,得到分析结果
	 * @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);
	    }
	    return content;
	}

	/**
	 * 转换天气情况
	 * @param s第1张图片
	 * @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.zdao.weather.bean;

public class Weather {
	//第1天时间
	private String date1;

	//第1天温度
	private String temperature1;

	//第1天天气情况
	private String weather1;

	//第1天风向
	private String wind1;

	//第2天时间
	private String date2;

	//第2天温度
	private String temperature2;

	//第2天天气情况
	private String weather2;

	//第2天风向
	private String wind2;

	//第3天时间
	private String date3;

	//第3天温度
	private String temperature3;

	//第3天天气情况
	private String weather3;

	//第3天风向
	private String wind3;

	//第4天时间
	private String date4;

	//第4天温度
	private String temperature4;

	//第4天天气情况
	private String weather4;

	//第4天风向
	private String wind4;

	//第5天时间
	private String date5;

	//第5天温度
	private String temperature5;

	//第5天天气情况
	private String weather5;

	//第5天风向
	private String wind5;

	public String getDate1() {
		return date1;
	}

	public void setDate1(String date1) {
		this.date1 = date1;
	}

	public String getDate2() {
		return date2;
	}

	public void setDate2(String date2) {
		this.date2 = date2;
	}

	public String getDate3() {
		return date3;
	}

	public void setDate3(String date3) {
		this.date3 = date3;
	}

	public String getDate4() {
		return date4;
	}

	public void setDate4(String date4) {
		this.date4 = date4;
	}

	public String getDate5() {
		return date5;
	}

	public void setDate5(String date5) {
		this.date5 = date5;
	}

	public String getTemperature1() {
		return temperature1;
	}

	public void setTemperature1(String temperature1) {
		this.temperature1 = temperature1;
	}

	public String getTemperature2() {
		return temperature2;
	}

	public void setTemperature2(String temperature2) {
		this.temperature2 = temperature2;
	}

	public String getTemperature3() {
		return temperature3;
	}

	public void setTemperature3(String temperature3) {
		this.temperature3 = temperature3;
	}

	public String getTemperature4() {
		return temperature4;
	}

	public void setTemperature4(String temperature4) {
		this.temperature4 = temperature4;
	}

	public String getTemperature5() {
		return temperature5;
	}

	public void setTemperature5(String temperature5) {
		this.temperature5 = temperature5;
	}

	public String getWeather1() {
		return weather1;
	}

	public void setWeather1(String weather1) {
		this.weather1 = weather1;
	}

	public String getWeather2() {
		return weather2;
	}

	public void setWeather2(String weather2) {
		this.weather2 = weather2;
	}

	public String getWeather3() {
		return weather3;
	}

	public void setWeather3(String weather3) {
		this.weather3 = weather3;
	}

	public String getWeather4() {
		return weather4;
	}

	public void setWeather4(String weather4) {
		this.weather4 = weather4;
	}

	public String getWeather5() {
		return weather5;
	}

	public void setWeather5(String weather5) {
		this.weather5 = weather5;
	}

	public String getWind1() {
		return wind1;
	}

	public void setWind1(String wind1) {
		this.wind1 = wind1;
	}

	public String getWind2() {
		return wind2;
	}

	public void setWind2(String wind2) {
		this.wind2 = wind2;
	}

	public String getWind3() {
		return wind3;
	}

	public void setWind3(String wind3) {
		this.wind3 = wind3;
	}

	public String getWind4() {
		return wind4;
	}

	public void setWind4(String wind4) {
		this.wind4 = wind4;
	}

	public String getWind5() {
		return wind5;
	}

	public void setWind5(String wind5) {
		this.wind5 = wind5;
	}
}
评论
Julian 2007-10-25
说说我的方法:我是抓的新浪的天气,抓回来后提供webservice。
webservice内有一个60分钟缓存。
本来打算作一个外部服务监控程序,就是对天气预报的提供商(新浪)进行定时监控,发现我的天气预报webservice歇菜了就马上给我发个mail.这时候先让webservice提供缓存里面的天气信息(1天的信息),然后在1天内修复这个文天。
目前看来大型网站的天气预报格式变化比较慢,至少6个月以上。
ice123456 2007-10-25
swiage 写道
我觉得 analysis 函数的返回值如果定义为 Vector 类型,使用起来可以更灵活;
private Vector search(String pattern, String match){
Pattern sp = Pattern.compile(pattern);
Matcher matcher = sp.matcher(match);
Vector myvector = new Vector();
while(matcher.find()){
myvector.add(matcher.group(1));
}
return myvector;
} 这样做我们就不必预先知道搜索结果的数量了。

不是我不想这样做 只是页面的布局是这样的
swiage 2007-10-24
我觉得 analysis 函数的返回值如果定义为 Vector 类型,使用起来可以更灵活;
private Vector search(String pattern, String match){
Pattern sp = Pattern.compile(pattern);
Matcher matcher = sp.matcher(match);
Vector myvector = new Vector();
while(matcher.find()){
myvector.add(matcher.group(1));
}
return myvector;
} 这样做我们就不必预先知道搜索结果的数量了。
ice123456 2007-10-20
gigix 写道
ice123456 写道
ladofwind 写道
这样太危险了吧,那边页面格式一变你这边程序就歇了,
我们以前用的是一个免费提供天气信息的web services服务,
直接在这边调用各种服务,能得到各种天气信息,
信息你在本地怎么处理都可以,包括显示方式啊什么的


难到问天网有提供天气信息的web services服务吗?? 在国内提供天气信息的基本没有web services服务
不要跟我说yahoo的, yahoo提供的是国外的web services,对中国的天气支持不是非常好,它有些信息都不正确

呵呵,那正好
你把这个信息清扫一下,用web services提供出来,不就方便别人了


有道理.
gigix 2007-10-19
ice123456 写道
ladofwind 写道
这样太危险了吧,那边页面格式一变你这边程序就歇了,
我们以前用的是一个免费提供天气信息的web services服务,
直接在这边调用各种服务,能得到各种天气信息,
信息你在本地怎么处理都可以,包括显示方式啊什么的


难到问天网有提供天气信息的web services服务吗?? 在国内提供天气信息的基本没有web services服务
不要跟我说yahoo的, yahoo提供的是国外的web services,对中国的天气支持不是非常好,它有些信息都不正确

呵呵,那正好
你把这个信息清扫一下,用web services提供出来,不就方便别人了
fkpwolf 2007-10-19
天气好像有通用的接口吧
比如mac osx上面的wigdet
vista上面也有类似的
cjp472 2007-10-19
功能不错,有参考价值,国外的webservice确实不合适
ice123456 2007-10-19
jarwang 写道
代码无法运行。

???
怎么可能
jarwang 2007-10-18
代码无法运行。
kenees 2007-10-18
ladofwind 写道
这样太危险了吧,那边页面格式一变你这边程序就歇了,
我们以前用的是一个免费提供天气信息的web services服务,
直接在这边调用各种服务,能得到各种天气信息,
信息你在本地怎么处理都可以,包括显示方式啊什么的
belivexiaoqi 2007-10-18
感觉code 有点乱,weather类构造的有点问题,

构造的weather类应该只有四个属性:date,temperature,weather,wind。

取哪天的天气,返回哪天的weather就ok了,不要一直不停的set,get.....看了太不舒服了。。
ice123456 2007-10-18
comliu 写道
除了用正则表达式,也可以采用Dom4j的XPath功能进行检索。

Dom4j?? 没有用过 介绍下?
comliu 2007-10-18
除了用正则表达式,也可以采用Dom4j的XPath功能进行检索。
ice123456 2007-10-18
ladofwind 写道
这样太危险了吧,那边页面格式一变你这边程序就歇了,
我们以前用的是一个免费提供天气信息的web services服务,
直接在这边调用各种服务,能得到各种天气信息,
信息你在本地怎么处理都可以,包括显示方式啊什么的


难到问天网有提供天气信息的web services服务吗?? 在国内提供天气信息的基本没有web services服务
不要跟我说yahoo的, yahoo提供的是国外的web services,对中国的天气支持不是非常好,它有些信息都不正确
eric chang 2007-10-17
ladofwind 写道
这样太危险了吧,那边页面格式一变你这边程序就歇了,
我们以前用的是一个免费提供天气信息的web services服务,
直接在这边调用各种服务,能得到各种天气信息,
信息你在本地怎么处理都可以,包括显示方式啊什么的
同意楼上的意见,这样的代码确实太不好了
ladofwind 2007-10-17
这样太危险了吧,那边页面格式一变你这边程序就歇了,
我们以前用的是一个免费提供天气信息的web services服务,
直接在这边调用各种服务,能得到各种天气信息,
信息你在本地怎么处理都可以,包括显示方式啊什么的
tomgreenintel 2007-10-17
如果对方把get方式给屏蔽掉。你怎么弄呢?没有根本灵活实现啊
InnocentBoy 2007-10-17
不过问天网不用登录的,哈哈!
ice123456 2007-10-17
Michael.zhl 写道
这样获得html是不是不太灵活?比如对方网站要求登陆,验证cookie

这方面到没有想过.... 谢谢你的意见
Michael.zhl 2007-10-17
这样获得html是不是不太灵活?比如对方网站要求登陆,验证cookie
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

ice123456
搜索本博客
博客分类
最近加入圈子
存档
最新评论
  • 自己一点心声
    我和老板的观点是一致的:有收益的投入才值得投入,so,我们经常重构,但我也拒绝过 ...
    -- by celine
  • 自己一点心声
    如果老板允许,但重构代码不得占用上班时间,不能影响到项目的进度,也不给你加班费, ...
    -- by Friedrich
  • 自己一点心声
    老板是不会去维护代码的。。。
    -- by shevliu
  • 自己一点心声
    C3PO 写道恕我直言,能把代码写得又精简又易懂的人很稀罕,可能一个公司也没几个 ...
    -- by protti
  • 自己一点心声
    就是啊,要是上头没有标准,每个人都搞一套,最后不乱套了吗?要是你是头儿,手下人为 ...
    -- by xyz20003