天天看点

Java 爬虫学习(二)关于 Jsoup 解析元素

Jsoup: 用来进行 Html 的 Dom 元素内容解析

官网: https://jsoup.org/

0. 项目准备

  • 项目结构 ?
Java 爬虫学习(二)关于 Jsoup 解析元素
  • pom.xml ?
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
	<groupId>org.jsoup</groupId>
	<artifactId>jsoup</artifactId>
	<version>1.8.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.8.1</version>
</dependency>
           

1. url 解析 ?

@Test
public void testUrl() throws Exception{
	// 解析 URL 第一个参数是 URL, 第二个参数是超时时间(单位是毫秒)
	Document doc = Jsoup.parse(new URL("http://www.itcast.cn"), 1000);

	// 使用标签选择器,获得网页中 title 标签内容
	String title = doc.getElementsByTag("title").first().text();

	System.out.println(title);
}
           

 2. file 解析 ?

@Test
public void testFile() throws Exception{
	Document doc = Jsoup.parse(new File("G:/IDEA/workspace/rc/src/main/resources/templates/rec-login.html"), "utf8");
	String title = doc.getElementsByTag("title").first().text();
	System.out.println(title);
}
           

3. dom 解析 ?

@Test
public void testDom() throws Exception{
	Document doc = Jsoup.parse(new File("G:/IDEA/workspace/rc/src/main/resources/templates/rec-login.html"), "utf8");
	// 根据 id 获取元素
//        Element element = doc.getElementById("registerForm");

	// 根据标签获取元素
//        Element element = doc.getElementsByTag("h2").first();

	// 根据 class 获取元素
//        Element element = doc.getElementsByClass("formPanel").first();

	// 根据属性获取元素
//        Element element = doc.getElementsByAttribute("method").first();

	// 根据属性及属性值获取元素
	Element element = doc.getElementsByAttributeValue("method", "post").first();

	System.out.println(element.text());
}
           

4. data 解析 ?

@Test
public void testData() throws Exception{
	Document doc = Jsoup.parse(new File("G:/IDEA/workspace/rc/src/main/resources/templates/rec-login.html"), "utf8");
	Element element = doc.getElementById("registerForm");

	String str = "";

	// 从元素中获取 id
//        str = element.id();
	// 从元素中获取 className
//        str = element.className();
	// 从元素中获取 attr
//        str = element.attr("id");
	// 从元素中获取所有 attr
//        str = element.attributes().toString();
	// 从元素中获取文本
	str = element.text();
	System.out.println(str);
}
           

5. selector 解析 ?

@Test
public void testSelector() throws Exception{
	Document doc = Jsoup.parse(new File("G:/IDEA/workspace/rc/src/main/resources/templates/rec-login.html"), "utf8");

	// el#id 查找带有该id的元素
//        Element element = doc.select("form#loginContent").first();
	// el.class 查找带有该class的元素
//        Element element = doc.select("div.form.login-form").first();
	// el[attr] 查找带有该属性的元素
//        Element element = doc.select("div[id]").first();
	// 任意组合 el[attr=val].class 查找带有该属性=属性值,该class的元素
//        Element element = doc.select("div[id=phizzForm].form.login-form").first();
	// ancestor child 查找父元素下的子元素
//        Element element = doc.select("#loginContent h2").first();
	// parent > child 查找父元素下的直接子元素
//        Element element = doc.select(".formPanel > p").first();
	// parent > * 查找父元素下的所有子元素
	for(Element e : doc.select("fieldset > *")){
		System.out.println(e.text());
	}
}
           

另外有关选择器的官方 API: https://jsoup.org/apidocs/org/jsoup/select/Selector.html