新浪天气预报接口 可一次查询多个城市天气

22 篇文章 0 订阅


可传入多个城市,用空格(%2C)分开


http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=0&city=%E5%8C%97%E4%BA%AC%2C%E4%B8%8A%E6%B5%B7&dfc=1&random=1434619840774


http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=0&city=北京%2C上海&dfc=1&random=1434619840774


返回:

(function() {
	var w = [];
	w['北京'] = [{
		s1: '多云',
		s2: '雷阵雨',
		f1: 'duoyun',
		f2: 'leizhenyu',
		t1: '31',
		t2: '19',
		p1: '3-4',
		p2: '≤3',
		d1: '北风',
		d2: '无持续风向'
	}];
	w['上海'] = [{
		s1: '小雨',
		s2: '多云',
		f1: 'xiaoyu',
		f2: 'duoyun',
		t1: '25',
		t2: '20',
		p1: '≤3',
		p2: '≤3',
		d1: '东北风',
		d2: '东北风'
	}];
	var add = {
		now: '2015-06-18 17:36:15',
		time: '1434620175',
		update: '北京时间06月18日08:20更新',
		error: '0',
		total: '2'
	};
	window.SWther = {
		w: w,
		add: add
	};
})(); //0  



	public static String cityweather(String city) {
		
		String url="";
		String vhtml="";
		String weather="";
		try {
			vhtml="这个是返回的html";
			System.out.println(vhtml);
			String city_s[]=city.split("\\+");
			String update=getSptderContent(vhtml, "1update:'([^<>,]+)',");
			for (int i = 0; i < city_s.length; i++) {
				
				Pattern p=Pattern.compile("("+city_s[i]+"'.*?;)");
				Matcher m=p.matcher(vhtml);
				if (m.find()) {
					String dd=m.group(1);
					String day_s1=getSptderContent(dd, "1s1:'([^<>,]+)',");//白天
					String day_s2=getSptderContent(dd, "1s2:'([^<>,]+)',");//晚上
					
					String day_t1=getSptderContent(dd, "1t1:'([^<>,]+)',");//白天温度
					String day_t2=getSptderContent(dd, "1t2:'([^<>,]+)',");//晚上温度
					
					String day_d1=getSptderContent(dd, "1d1:'([^<>,]+)',");//白天
					String day_d2=getSptderContent(dd, "1d2:'([^<>,]+)',");//晚上
					
					String day_p1=getSptderContent(dd, "1p1:'([^<>,]+)',");//白天风速
					String day_p2=getSptderContent(dd, "1p2:'([^<>,]+)',");//晚上
					
					weather+="【"+city_s[i]+"】白天:"+day_s1+day_t1+"℃ "+day_d1+day_p1+"级,夜间:"+day_s2+day_t2+"℃ "+day_d2+day_p2+"级;";
				}
			}
			//System.out.println(weather);
			return weather+update;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return weather;
	}


	public static String getSptderContent(String text,String reg){
		String content="";
		int cou=Integer.parseInt(reg.substring(0,1));
		String regex=reg.substring(1,reg.length());
		
		Pattern p2 = Pattern.compile(regex);   
		Matcher m2 = p2.matcher(text);   
		 
		    if(m2.find()){
		    	content=m2.group(cou);     
		 	}
		    String regEx_html = "<[^>]+>";
		    if (content!=null) {
		    	content=content.replaceAll(regEx_html, "");
			}
		    return content;
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值