jsoup获取html页面中的内容

来源:互联网 发布:开源实时数据库有哪些 编辑:程序博客网 时间:2024/06/06 16:44

当我想要获取<a sf="pagebar" sf:data="({pg:1,ps:1000,tt:9091,pn:5,pc:10,id:'',st:true})"></a>这行数据时,按平常的doc.select(".clearfix").get(2)是行不通的,需要换一种写法:

Elements item=doc.select("a[sf=pagebar]");
            String data=item.attr("sf:data");
            
            JSONObject json=JSONObject.parseObject(data.substring(1,data.length()-1));
            int tt=json.getIntValue("tt");
            int ps=json.getIntValue("ps");


想要获取script标签里的数据时:

Element item=doc.select(".clearfix").get(2);
            Elements eles = item.getElementsByTag("script");
            for (Element ele : eles) {    // 檢查是否有$total字串
                String script = ele.toString();
                if (script.indexOf("$total") > -1) {
                    script = ele.childNode(0).toString();   // 只取得script的內容
                    String countPage=script.substring(script.indexOf("$total")+8,script.indexOf("$reload")-2);
                }
            }
           

0 0
原创粉丝点击