胜煜的博客

心随所向,无问西东

Soup解析html简单使用

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class TestSinaHtml_Soup {
    
    public static void main(String[] args){
        final String url="http://news.sina.com.cn/o/2017-12-17/doc-ifypsvkp4239244.shtml" ;
        try {
            Document doc = Jsoup.connect(url).header("Accept", "*/*")
                    .header("Accept-Encoding", "gzip, deflate")
                    .header("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                    .header("Referer", "https://www.baidu.com/")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
                    .timeout(5000)
                    .get();

            Elements container = doc.getElementsByClass("article-content clearfix");//class属性
            Document containerDoc = Jsoup.parse(container.toString());
            Elements module = containerDoc.getElementsByClass("article-content-left");
            Document moduleDoc = Jsoup.parse(module.toString());

//            Elements divsElements = doc.select("table");//得到table标签中的内容  
//            Elements divs = doc.select("div#content_left div[data-tools]");
            

            Elements clearfix = moduleDoc.select(".article");  //选择器的形式
            Document detail = Jsoup.parse(clearfix.toString());
            //输出html信息
            System.out.println("--------------------------------------html-----------------------------------------");
            System.out.println(detail);
            System.out.println("--------------------------------------html-----------------------------------------");
            
            //输出标题
            System.out.println("--------------------------------------title-----------------------------------------");
            Elements divs = detail.select("strong");
            for (Element div : divs){
                System.out.println(div.text());
            }
            
            Elements pngs = detail.select("img[src~=.(gif|jpg)]");

            for(Element png:pngs){  
                String pngText=png.text();
                String src=png.attr("src");//根据属性名获取src的路径  
                System.out.println(src+pngText);  
            }  
            
            //获取href路径
            System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            Elements links = doc.getElementsByTag("a");
            for (Element link : links) {
              link.attr("href",link.attr("abs:href"));//绝对路径
              String linkHref = link.attr("href");
              System.out.println(linkHref);
              String linkText = link.text();
              System.out.println(linkText);
            }
            
            
          /* Elements ele=doc.getElementsByTag("p");//String  html="<p><a href=\"a.html\">a</p><p> 文本</p>";
            for(Element e :ele){
                System.out.println(e.text());
            }*/
            
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
      
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011366045/article/details/79976850
文章标签: java soup html
个人分类: 开发总结
所属专栏: 开发总结
上一篇计算机主板的几种高速率接口了解
下一篇jdk8新增Lambda及相关使用
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭