天气情况获取

不解释,先上代码


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedList;
import java.util.List;

/**
* @version: 1.00.00
* @description: 描述:获取天气情况
* @company: 
* @author: 
* @date: 2021-12-21 15:33
*/
@Slf4j
public class WeatherUtil {

    /* 数据观测时间 */
    public static final String OBSTIME = "obsTime";
    /* 图片编码 */
    public static final String ICON = "icon";
    /* 编码 */
    public static final String ADCODE = "adcode";
    /* 天气状况 */
    public static final String WEATHER = "text";
    /* 温度 */
    public static final String TEMPERATURE = "temp";
    /* 体感温度 */
    public static final String FEELSLIKE = "feelsLike";
    /* 风向360角度 */
    public static final String WIND360 = "wind360";
    /* 风向 */
    public static final String WINDDIR = "windDir";
    /* 风级 */
    public static final String WINDSCALE = "windScale";
    /* 风速 */
    public static final String WINDSPEED = "windSpeed";
    /* 湿度 */
    public static final String HUMIDITY = "humidity";
    /* 降水量 */
    public static final String PRECIP = "precip";
    /* 大气压强,默认单位:百帕 */
    public static final String PRESSURE = "pressure";
    /* 能见度,默认单位:公里 */
    public static final String VIS = "vis";
    /* 云量,百分比数值。可能为空 */
    public static final String CLOUD = "cloud";
    /* 露点温度。可能为空 */
    public static final String DEW = "dew";

    public static void main(String[] args) throws IOException {
        getWeather("101060604");
    }

    /**
     * 和风天气api
     * 根据城市ID()获取实时天气
     * <p>
     *     gzip=n  解决乱码
     *     当前 key : e093c40563************90bddbca7ae976d
     *     key值为注册和风天气,官方提供免费,每天上限调用10000次
     *     如果失效,重新去申请  <a href = "https://console.qweather.com/#/apps" />
     * </p>
     * @param id:选取和风天气提供的城市编码
     */
    public static JSONObject getWeather(String id){
        try {
            log.info("-----------------------------获取和风天气接口开始----------------------");
            URL url = new URL("https://devapi.qweather.com/v7/weather/now?location=" + id + "&key=e093c************a7ae976d&gzip=n");
            URLConnection connectionData = url.openConnection();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    connectionData.getInputStream(), "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null){
                sb.append(line);
            }
            String data = sb.toString();
            JSONObject json = JSONUtil.parseObj(data).getJSONObject("now");
            br.close();
            log.info("-----------------------------获取和风天气接口结束----------------------");
            return json;
        } catch (Exception e) {
            log.info("-----------------------------获取和风天气接口 ERROR----------------------");
            e.printStackTrace();
            return null;
        }
    }

获取大范围时间内的天气情况(现在用不了,得稍微修改下)


    /**
     * 获取七天天气
     * @param city:城市编码、中国天气网的编码
     */
    public void getSevenWeather(String city){
        //--------------------------------------------此方法需要的时候建议也改成上面那个用和风天气------------------------------------------
        log.info("-----------------------------获取中国天气局接口开始----------------------");
        //        String url = "http://www.weather.com.cn/weather/" +city  + ".shtml";
        //        String url = "http://wthrcdn.etouch.cn/weather_mini?city=汨罗市";
        // 高德接口
        // https://restapi.amap.com/v3/weather/weatherInfo?city=430681&key=f09391e22*62b2cf94de8
        // https://restapi.amap.com/v3/weather/weatherInfo?key=f09391e22*cf94de8&city="+city+"&extensions=base&output=JSON
        city="101190101";
        String url = "http://www.weather.com.cn/weather/" +city  + ".shtml";
        List<String>lweather=new LinkedList<String>();//用于存储天气状况
        List<String>lweatherData=new LinkedList<String>();//用于存储日期
        List<String>lweatherTempture=new LinkedList<String>();//用于存储温度
        List<String>lweatherWin=new LinkedList<String>();//用于存储风向
        List<String>lweather_ALl=new LinkedList<String>();
        try {
            Document doc = Jsoup.connect(url).get();
            Elements content = doc.getElementsByClass("t clearfix");
            for (Element e : content) {
//                Document conDoc = Jsoup.parse(e.toString());
//                Elements cru = conDoc.getElementsByClass("crumbs fl");
                Elements sky = content.select("li[class^=sky skyid lv]");
                for (Element e1 : sky) {
                    Elements weatherData=e1.select("h1");//此处用于获取日期天气
                    lweatherData.add(weatherData.text());
                    Elements weather=e1.select("p[class=wea]");//用于获取天气信息
                    lweather.add(weather.text());
                    Elements weatherTempture= e1.select("p[class=tem]");//用于获取天气温度
                    lweatherTempture.add(weatherTempture.text());
                    Elements weatherWin=e1.select("span");//用于获取风向
                    lweatherWin.add(weatherWin.attr("title"));
                }
            }
        } catch (Exception e) {
            log.info("-----------------------------获取中国天气局接口 ERROR----------------------");
            e.printStackTrace();
        }
//        System.out.println("天气查询完毕!!");
//        System.out.println("当前城市【"+"汨罗"+"】");
        for(int i=0;i<lweather.size();i++) {
            String wea_All="日期:"+lweatherData.get(i)+",天气状况:"+lweather.get(i)+",温度:"+lweatherTempture.get(i)+",风向:"+lweatherWin.get(i);
            lweather_ALl.add(wea_All);
            //System.out.println("日期:"+lweatherData.get(i)+",天气状况:"+lweather.get(i)+",温度:"+lweatherTempture.get(i)+",风向:"+lweatherWin.get(i));
        }
        for(String weaAl:lweather_ALl) {
            System.out.println(weaAl);
        }
        log.info("-----------------------------获取中国天气局接口结束----------------------");
    }
}

城市编码

百度网盘
和风api:https://pan.baidu.com/s/19zTM9jCHstCdyBCKQMIJBA 提取码:yuge
中国天气网的编码好像同和风api的一样
高德api: https://pan.baidu.com/s/19Q1YsLrlcuF_xkCaC6EBxA 提取码:yuge

key值

百度:和风天气api接口,去那自己注册申请去,免费的;
高德也是一样的;

posted @ 2021-12-22 22:18  窃窃私语QAQ  阅读(86)  评论(0编辑  收藏  举报