获取天气信息的工具类

获取天气信息的工具类

jar包下载:
链接:https://pan.baidu.com/s/1nM1C-WQMsKNpxM2xJEgz1g
提取码:r540

package cron;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class WeatherforecastUtils {
	   public Document getHtml(String url){
	        Map map = new HashMap<>();
	        map.put("165.227.102.37", 3128);
	        map.put("198.211.109.90", 8080);
	        map.put("68.183.16.193", 8080);

	        CloseableHttpClient httpClient = HttpClients.createDefault();
	        HttpGet get = null;
	        try {
//	            get.setConfig(
//	                    RequestConfig.custom().setProxy(new HttpHost("68.183.16.193", 8080, "HTTP")).build()
//	            );
	            get = new HttpGet(url);
	            get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0");
	            CloseableHttpResponse response = httpClient.execute(get);
	            int code = response.getStatusLine().getStatusCode();
	            System.out.println(code);
	            HttpEntity entity = response.getEntity();
	            String content = EntityUtils.toString(entity, "UTF-8");
	            Document document = Jsoup.parse(content);
	            return document;
	        }catch (Exception e){
	            Document document = null;
	            get = new HttpGet(url);
	            for(Object k:map.keySet())
	            {
	                get.setConfig(
	                        RequestConfig.custom().setProxy(new HttpHost(k.toString(), (Integer) map.get(k.toString()), "HTTP")).build()
	                );
	                CloseableHttpResponse response = null;
	                try {
	                    response = httpClient.execute(get);
	                    int code = response.getStatusLine().getStatusCode();
	                    if(code == 200){
	                        HttpEntity entity = response.getEntity();
	                        String content = EntityUtils.toString(entity, "UTF-8");
	                        document = Jsoup.parse(content);
	                        break;
	                    }
	                }catch (Exception o){
	                    document = null;
	                }
	            }
	            return document;
	        }
	    }

	    public String getWeather(){
	        try {
	            String url = "https://weather.com/zh-CN/weather/today/l/7f14186934f484d567841e8646abc61b81cce4d88470d519beeb5e115c9b425a";

	            Document document = getHtml(url);

	            Element element1 = document.select("div.today_nowcard-temp > span").first();
	            String temperature = element1.text();     //现在温度

	            Element element2 = document.select("div.today_nowcard-phrase").first();
	            String cloud = element2.text();    //天气状况

	            Element element3 = document.select("div.today_nowcard-feels").first();
	            String body_temperature = element3.text();   //体感温度

	            Element element4 = document.select("div.today_nowcard-hilo").first();
	            String max_min = element4.text();   //最高最低温度和紫外线

	            Element element5 = document.getElementsByClass("today_nowcard-sidecar component panel").first();
	            Elements elements = element5.getElementsByTag("span");

	            String wind = elements.get(0).text();    //风力
	            String humidity = elements.get(1).text();     //湿度
	            String rain_dew = elements.get(4).text();    //露点
	            String pressure = elements.get(5).text();    //气压
	            String visibility = elements.get(7).text();  //能见度

	            //转化json
	            JSONObject result = new JSONObject();
	            result.put("code", 200);
	            JSONObject weather = new JSONObject();
	            weather.put("temperature", temperature);
	            weather.put("cloud", cloud);
	            weather.put("body_temperature", body_temperature);
	            weather.put("max_min", max_min);
	            weather.put("wind", wind);
	            weather.put("humidity", humidity);
	            weather.put("rain_dew", rain_dew);
	            weather.put("pressure", pressure);
	            weather.put("visibility", visibility);
	            JSONArray jsonArray = new JSONArray();
	            jsonArray.add(0, weather);
	            result.element("data", jsonArray);
	            return  result.toString();

	        }catch (Exception e){
	            return null;
	        }
	    }


}

结果:

{"code":200,"data":[{"temperature":"28°","cloud":"大部多云","body_temperature":"体感温度 29°","max_min":"高温 29° / 低温 22° 紫外线指数 5(最大值10)","wind":"东北 18 公里 /小时","humidity":"52%","rain_dew":"17°","pressure":"1,013.2 毫巴","visibility":"9.7 公里"}]}

可以根据自己想要的信息在工具类中进行筛选。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

渡生远方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值