解析html

1下载jsoup.jar包 https://jsoup.org/download

2解析基础天气情况

    try{
        int code = 101110103;//西安市临潼区编码
        Document doc = Jsoup.connect("http://www.weather.com.cn/weather1d/" + code + ".shtml").get();
        Elements title = doc.select("div.t");
        //当天温度 天气现象 风级 日出日落时间
        Log.d("debug1",title.get(0).text());

        Elements title1 = doc.select("div.livezs");
        //生活指数 紫外线 感冒指数 穿衣指数 洗车指数 空气质量指数           
        Log.d("debug1",title1.get(0).text());

        Elements title2 = doc.select("input");

        Log.d("debug1",title2.size() + "");
        int i = 0;
        for (Element element : title2) {
            if(i == 7){
                Log.d("debug1",element + "");
            //更新时间 天气现象 温度范围     
                Log.d("debug1",element.toString().substring(46,73));
            }
            i++;
        }
    }catch(Exception e)
                {
                    Log.d("debug1", "error");

                }

3二十四小时预报

int code = 101110103;
 Document doc = Jsoup.connect("http://www.weather.com.cn/weather1d/" + code + ".shtml").get();
Elements title = doc.select("script");
        int i = 0;
        for (Element element : title) {
            if (i == 4) {
                String string = element.toString().substring(79);
                String[] split2 = string.split(",");
                for (String string3 : split2) {
                    String[] split3 = string3.split(":");
                    for (int j = 0; j < split3.length; j++) {
                        if (!(j % 2 == 0)) {
                            if(!(j%15 == 0)) {
                            //时间倒序 温度 角度?风向 风速 降雨量  相对湿度
                                Log.d("debug1", split3[j].substring(1, split3[j].length() - 1));
                            }
                        }
                    }
                }

                //}
            }
            i++;
        }

4八天预报

try{
             int code = 101110103;
              Document doc = Jsoup.connect("http://www.weather.com.cn/weather1d/" + code + ".shtml").get();
Elements title = doc.select("li");
        int i = 0;
        for (Element element : title) {
            if(i >= 9 && i <17){
                Elements select = element.select("span.time");
                //时间
                Log.d("debug1",select.get(0).text());
                Elements select1 = element.select("span.tem");
               //温度范围
                Log.d("debug1",select1.get(0).text());
                Elements select2 = element.select("span.wind");
                //风向
                Log.d("debug1",select2.get(0).text());
                Elements select3 = element.select("span.wind1");
                //风速
                Log.d("debug1",select3.get(0).text());
            }
            i++;
        }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("error!");
            e.printStackTrace();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值