利用爬虫后端根据地名获取最近天气情况

后端根据地名获取最近天气情况


话不多说直接上代码:

public class Test {
public static void main(String[] args) {
String s = exp1(“奉化”);
System.out.println(s);
}
static String exp1(String cityName){
StringBuilder sb=new StringBuilder();
try {
URL url=new URL(“http://wthrcdn.etouch.cn/weather_mini?city=”+cityName);
URLConnection urlConnection=url.openConnection();
InputStream is=urlConnection.getInputStream();
GZIPInputStream gzin = new GZIPInputStream(is);
InputStreamReader isr=new InputStreamReader(gzin,“utf-8”);
BufferedReader br=new BufferedReader(isr);
String str=null;
while ((str=br.readLine())!=null){
sb.append(str);
}
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
输出结果
在这里插入图片描述
{“data”:{“yesterday”:{“date”:“3日星期二”,“high”:“高温 19℃”,“fx”:“北风”,“low”:“低温 11℃”,“fl”:"<![CDATA[3级]]>",“type”:“晴”},“city”:“奉化”,“forecast”:[{“date”:“4日星期三”,“high”:“高温 19℃”,“fengli”:"<![CDATA[2级]]>",“low”:“低温 13℃”,“fengxiang”:“北风”,“type”:“多云”},{“date”:“5日星期四”,“high”:“高温 21℃”,“fengli”:"<![CDATA[1级]]>",“low”:“低温 14℃”,“fengxiang”:“东南风”,“type”:“阴”},{“date”:“6日星期五”,“high”:“高温 25℃”,“fengli”:"<![CDATA[2级]]>",“low”:“低温 13℃”,“fengxiang”:“北风”,“type”:“晴”},{“date”:“7日星期六”,“high”:“高温 22℃”,“fengli”:"<![CDATA[2级]]>",“low”:“低温 13℃”,“fengxiang”:“北风”,“type”:“多云”},{“date”:“8日星期天”,“high”:“高温 19℃”,“fengli”:"<![CDATA[3级]]>",“low”:“低温 11℃”,“fengxiang”:“北风”,“type”:“晴”}],“ganmao”:“感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。”,“wendu”:“18”},“status”:1000,“desc”:“OK”}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值