Jsoup(HTML解析)
1.Jsoup(HTML解析)
https://blog.csdn.net/qq_26786441/article/details/106207828
2.Jsoup解析html
https://blog.csdn.net/m0_66144992/article/details/122329560
1、环境
首先导入jar包
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
然后准备一个用于解析的jsoup.html,放于webapp目录下
<html>
<head>
<title>传智播客官网-一样的教育,不一样的品质</title>
</head>
<body>
<div class="city">
<h3 id="city_bj">北京中心</h3>
<fb:img src="/2018czgw/images/slogan.jpg" class="slogan"/>
<div class="city_in">
<div class="city_con" style="display: none;">
<ul>
<li id="test" class="class_a class_b">
<a href="http://www.itcast.cn" target="_blank">
<span class="s_name">北京</span>
</a>
</li>
<li>
<a href="http://sh.itcast.cn" target="_blank">
<span class="s_name">上海</span>
</a>
</li>
<li>
<a href="http://gz.itcast.cn" target="_blank">
<span abc="123" class="s_name">广州</span>
</a>
</li>
<ul>
<li>天津</li>
</ul>
</ul>
</div>
</div>
</div>
</body>
</html>
2、3种获取Dom方式
2.1解析url
@Test public void testJsoupUrl() throws Exception { //解析url地址 Document document = Jsoup.parse(new URL("http://www.itcast.cn/"), 1000); //获取title的内容 Element title = document.getElementsByTag("title").first(); System.out.println(title.text()); }
虽然使用Jsoup可以替代HttpClient直接发起请求解析数据,但是往往不会这样用,因为实际的开发过程中,需要使用到多线程,连接池,代理等等方式,而jsoup对这些的支持并不是很好,
所以我们一般把jsoup仅仅作为Html解析工具使用
2.2解析字符串
@Test public void testJsoupString() throws IOException { //读取文件获取 //String html = FileUtils.readFileToString(new File("D:\\jsoup.html"), "UTF-8"); String html = IOUtils.toString(new FileInputStream("src/main/webapp/jsoup.html"), "UTF-8"); //解析字符串 Document document = Jsoup.parse(html); //获取title的内容 Element title = document.getElementsByTag("title").first(); System.out.println(title.text()); }
2.3直接解析文件
@Test public void testJsoupHtml() throws IOException { //解析文件 // Document document = Jsoup.parse(new File("D:\\jsoup.html"),"UTF-8"); Document document = Jsoup.parse(new FileInputStream("src/main/webapp/jsoup.html"), "UTF-8", ""); //获取title内容 Element title = document.getElementsByTag("title").first(); System.out.println(title.text()); }
3、解析Dom方法
3.1dom方式遍历文档,获取元素
@Test public void testDoucment() throws IOException { String html = IOUtils.toString(new FileInputStream("src/main/webapp/jsoup.html"), "UTF-8"); Document document = Jsoup.parse(html); //1.根据id查询元素getElementById Element element = document.getElementById("city_bj"); System.out.println(element.text()); //2.根据标签获取元素getElementsByTag element = document.getElementsByTag("title").first(); System.out.println(element.text()); //3.根据class获取元素getElementsByClass //element = document.getElementsByClass("class_a").last(); // element = document.getElementsByClass("class_b").last(); element = document.getElementsByClass("class_a class_b").last(); System.out.println(element.text()); //4.根据属性获取元素getElementsByAttribute // element = document.getElementsByAttribute("abc").first(); element = document.getElementsByAttributeValue("abc", "123").first(); System.out.println(element.text()); }
3.2元素中获取数据
@Test public void testData() throws Exception { String html = IOUtils.toString(new FileInputStream("src/main/webapp/jsoup.html"), "UTF-8"); Document document = Jsoup.parse(html); Element element = document.getElementById("test"); //1.从元素中获取id String id = element.id(); System.out.println(id); //2.从元素中获取className String className = element.className(); System.out.println(className); //3.从元素中获取属性的值attr id = element.attr("id"); System.out.println(id); //4.从元素中获取所有属性attributes Attributes attributes = element.attributes(); String s = attributes.toString(); System.out.println(s); //5.从元素中获取文本内容text System.out.println(element.text()); }
3.3select,css查找
@Test public void testSelector() throws IOException { String html = IOUtils.toString(new FileInputStream("src/main/webapp/jsoup.html"), "UTF-8"); Document document = Jsoup.parse(html); //tagname: 通过标签查找元素,比如:span Elements elements = document.select("span"); elements.forEach(element -> System.out.println(element.text())); //#id: 通过ID查找元素,比如:#city_bjj String text = document.select("#city_bj").text(); //.class: 通过class名称 text = document.select(".class_a").text(); //[attribute]: 利用属性查找元素,比如:[abc] text = document.select("[abc]").text(); //[attr=value]: 利用属性值来查找元素,比如:[class=s_name] text = document.select("[class=s_name]").text(); }
3.4select,css组合查找
@Test public void testSelectors() throws IOException { String html = IOUtils.toString(new FileInputStream("src/main/webapp/jsoup.html"), "UTF-8"); Document document = Jsoup.parse(html); //el#id: 元素+ID,比如: h3#city_bj String str = document.select("h3#city_bj").text(); System.out.println(str); //el.class: 元素+class,比如: li.class_a System.out.println(document.select("li.class_a").text()); //el[attr]: 元素+属性名,比如: span[abc] System.out.println(document.select("span[abc]").text()); //任意组合: 比如:span[abc].s_name System.out.println(document.select("span[abc].s_name").text()); //ancestor child: 查找某个元素下子元素,比如:.city_con li 查找"city_con"下的所有li System.out.println(document.select(".city_con li").text()); //parent > child: 查找某个父元素下的直接子元素,比如: //.city_con > ul > li 查找city_con第一级(直接子元素)的ul,再找所有ul下的第一级li System.out.println(document.select(".city_con > ul > li").text()); //parent > *: 查找某个父元素下所有直接子元素 System.out.println(document.select(".city_con > *").text()); }

浙公网安备 33010602011771号