zoukankan      html  css  js  c++  java
  • Jsoup(三)-- Jsoup使用选择器语法查找DOM元素

    1.Jsoup可以使用类似于CSS或jQuery的语法来查找和操作元素.

    2.实例如下:

        public static void main(String[] args) throws Exception{
            // 创建httpClient实例
            CloseableHttpClient httpClient = HttpClients.createDefault();
            // 创建httpGet实例
            HttpGet httpGet = new HttpGet("http://www.cnblogs.com");
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
            CloseableHttpResponse response = httpClient.execute(httpGet);
            String content = null;
            if(response != null){
                HttpEntity entity = response.getEntity();   
                content = EntityUtils.toString(entity, "UTF-8");  // 获取网页内容
                Document document = Jsoup.parse(content);  // 解析网页,得到文档对象
                
                // 1.查找所有帖子DOM
                Elements elements = document.select(".post_item .post_item_body h3 a");
                for(Element ele : elements){
                    System.out.println("博客标题:" + ele.text());
                }
                System.out.println("------------------------分割线------------------------");
                
                // 2.查找带有href属性的a元素
                Elements hrefElements = document.select("a[href]");
                for(Element ele : hrefElements){
                    System.out.println(ele.toString());
                }
                System.out.println("------------------------分割线------------------------");
                
                // 3.查找扩展名为.png的图片DOM节点
                Elements imgElements = document.select("img[src$=.png]");
                for(Element ele : imgElements){
                    System.out.println(ele.toString());
                }
                System.out.println("------------------------分割线------------------------");
                
                // 4.获取tag为title的第一个DOM元素
                Element titleEle = document.getElementsByTag("title").first();
                System.out.println("标题为:" + titleEle.text());
            }
            if(response != null){
                response.close();
            }
            if(httpClient != null){
                httpClient.close();
            }
        }

    3.Jsoup学习地址

      开源博客系统-Jsoup

  • 相关阅读:
    安卓开发_求好评功能
    安卓开发_深入理解Content Provider
    安卓开发_数据存储技术_sqlite
    安卓开发_慕课网_Fragment实现Tab(App主界面)
    安卓开发_数据存储技术_外部存储
    Go语言基础之数组
    Go语言基础之结构体
    Go操作MySQL
    Go语言基础之文件操作
    Go第三方日志库logrus
  • 原文地址:https://www.cnblogs.com/xbq8080/p/7528909.html
Copyright © 2011-2022 走看看