当前位置: 首页 > 工具软件 > Jsoupxpath > 使用案例 >

xpath与jsoup

吴同
2023-12-01
Document document = Jsoup.parse(new URL("https://blog.csdn.net/u014172271"), 1000);
        Element title = document.getElementsByTag("title").first();
        System.out.println(title.text());
        /**
         * 1.	根据id查询元素getElementById
         * 2.	根据标签获取元素getElementsByTag
         * 3.	根据class获取元素getElementsByClass
         * 4.	根据属性获取元素getElementsByAttribute
         *
         *  Elements skuEles = spuEle.select("li.ps-item");
         *  String picUrl ="https:"+ skuEle.select("img[data-sku]").first().attr("data-lazy-img");
         */
        String path = ItCastSpidder.class.getClassLoader().getResource("index.html").getPath();
        document = Jsoup.parse(new File(path), "UTF-8");
        //获取title的内容
        title = document.getElementsByTag("title").first();
        System.out.println(title.text());
        Element element = document.getElementById("city_bj");
        //2.   根据标签获取元素getElementsByTag
        element = document.getElementsByTag("title").first();

        //3.   根据class获取元素getElementsByClass
        element = document.getElementsByClass("s_name").last();

        //4.   根据属性获取元素getElementsByAttribute
        element = document.getElementsByAttribute("abc").first();
        element = document.getElementsByAttributeValue("class", "city_con").first();

        //tagname: 通过标签查找元素,比如:span
        Elements span = document.select("span");
        for (Element element1 : span) {
            System.out.println(element1.text());
        }

        //#id: 通过ID查找元素,比如:#city_bjj
        String str = document.select("#city_bj").text();

        //.class: 通过class名称查找元素,比如:.class_a
        str = document.select(".class_a").text();
        System.out.println(".class_a" + str);

        //[attribute]: 利用属性查找元素,比如:[abc]
        str = document.select("[abc]").text();
        System.out.println("[abc]" + str);

        //[attr=value]: 利用属性值来查找元素,比如:[class=s_name]
        str = document.select("[class=s_name]").text();
        System.out.println("[class=s_name]" + str);

        //el#id: 元素+ID,比如: h3#city_bj
        str = document.select("h3#city_bj").text();
        System.out.println("[h3#city_bj]" + str);

        //el.class: 元素+class,比如: li.class_a
        str = document.select("li.class_a").text();
        System.out.println("[li.class_a]" + str);

        //el[attr]: 元素+属性名,比如: span[abc]
        str = document.select("span[abc]").text();
        System.out.println("[span[abc]]" + str);

        //任意组合,比如:span[abc].s_name
        str = document.select("span[abc].s_name").text();
        System.out.println("[span[abc].s_name]" + str);

        //ancestor child: 查找某个元素下子元素,比如:.city_con li 查找"city_con"下的所有li
        str = document.select(".city_con li").text();
        System.out.println("[.city_con li]" + str);

        //parent > child: 查找某个父元素下的直接子元素,
        //比如:.city_con > ul > li 查找city_con第一级(直接子元素)的ul,再找所有ul下的第一级li
        str = document.select(".city_con > ul > li").text();
        System.out.println("[.city_con > ul > li]" + str);

        //parent > * 查找某个父元素下所有直接子元素.city_con > *
        str = document.select(".city_con > *").text();
        System.out.println(".city_con > *" + str);
page.getHtml().xpath("//div[@class=mt]/h1/text()")
page.getHtml().css("div.mt>h1").toString()
page.getHtml().css("div#news_div > ul > li:nth-child(1) a").toString()
//div[contains(@class, "className")]

参考文档:
https://www.bilibili.com/video/BV1QV41127Wk?p=19

 类似资料: