@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());
}