JAVA爬取天气数据

393 阅读2分钟
  • 持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第30天,点击查看活动详情

天气数据获取

天气数据可以通过天气网或者百度的开放api来获取天气信息内容,也可以通过爬取来获取基础的天气信息,百度的天气网站有防爬机制,如果不想麻烦的破解就可以基于百度搜索的结果来做天气信息的基础获取,比如要获取北京的天气直接搜索北京天气,百度的第一个会百度天气的内容

image.png

基于这些给的基础信息来获取天气数据,获取此页面的Document并获取最高级的Elements

image.png

Document document = null;
try {
    document = Jsoup.connect(WEATHERURL + city + "天气").userAgent(userAgent).timeout(1000).get();
} catch (IOException e) {
    throw new RuntimeException(e);
}
StringBuffer buffer = new StringBuffer();
Elements es = document.getElementsByClass("wrapper_l wrapper_new");

获取最高级Elements后循环获取需要的Element元素,想要实时的天气数据就需要找到实时的Element元素

image.png

这里调侃一下class命名,实时就叫shishi,在循环里进行判断,如果这个元素不为空时转为text的内容

for (Element e:es) {
    // 实时温度
    if (!e.getElementsByClass("op_weather4_twoicon_shishi_info").isEmpty()){
        String realTemperature = e.getElementsByClass("op_weather4_twoicon_shishi_info").text();
    }
}

获取今日温度区间元素,这个温度区间是多个的,所以我们需要获取第一个元素的内容

image.png

// 今日温度
if (!e.getElementsByClass("op_weather4_twoicon_temp").isEmpty()){
    String todayTemperature = e.getElementsByClass("op_weather4_twoicon_temp").get(0).text();
    buffer.append("今日温度:"+todayTemperature+"\r");
}

其他内容同样,想要什么内容获取不同元素的结果,代码如下

public class WeatherCrawling {

    private static final String WEATHERURL = "http://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=";

    public static String getWeather(String city){
        String userAgent = String.valueOf(System.currentTimeMillis());
        Document document = null;
        try {
            document = Jsoup.connect(WEATHERURL + city + "天气").userAgent(userAgent).timeout(1000).get();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        StringBuffer buffer = new StringBuffer();
        Elements es = document.getElementsByClass("wrapper_l wrapper_new");
        for (Element e:es) {
            // 实时温度
            if (!e.getElementsByClass("op_weather4_twoicon_shishi_info").isEmpty()){
                String realTemperature = e.getElementsByClass("op_weather4_twoicon_shishi_info").text();
                buffer.append("实时天气:"+realTemperature+"\n");
            }
            // 今日温度
            if (!e.getElementsByClass("op_weather4_twoicon_temp").isEmpty()){
                String todayTemperature = e.getElementsByClass("op_weather4_twoicon_temp").get(0).text();
                buffer.append("今日温度:"+todayTemperature+"\n");
            }
            // 今日天气
            if (!e.getElementsByClass("op_weather4_twoicon_weath").isEmpty()){
                String todayWeather = e.getElementsByClass("op_weather4_twoicon_weath").get(0).text();
                buffer.append("今日天气:"+todayWeather+"\n");
            }
            // 风力风向
            if (!e.getElementsByClass("op_weather4_twoicon_wind").isEmpty()){
                String todayWind = e.getElementsByClass("op_weather4_twoicon_wind").get(0).text();
                buffer.append("今日风级:"+todayWind+"\n");
            }
        }
        System.out.println(buffer);
        return buffer.toString();
    }

    public static void main(String[] args) {
        getWeather("北京");
    }
}

运行main方法查看结果

image.png