Skip to content

Commit c271755

Browse files
lsoaresKevinGilmore
authored andcommitted
Initial commit of Jsoup example (eugenp#913)
* Initial commit of Jsoup example * Initial commit of Jsoup example * Fix the requested changes
1 parent 428bfcc commit c271755

3 files changed

Lines changed: 123 additions & 4 deletions

File tree

‎jsoup/pom.xml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>parent-modules</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>jsoup</artifactId>
10+
<packaging>jar</packaging>
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.jsoup</groupId>
14+
<artifactId>jsoup</artifactId>
15+
<version>${jsoup.version}</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>commons-io</groupId>
19+
<artifactId>commons-io</artifactId>
20+
<version>${commons.io.version}</version>
21+
</dependency>
22+
</dependencies>
23+
<properties>
24+
<maven.compiler.source>1.8</maven.compiler.source>
25+
<maven.compiler.target>1.8</maven.compiler.target>
26+
<commons.io.version>2.5</commons.io.version>
27+
<jsoup.version>1.10.1</jsoup.version>
28+
</properties>
29+
</project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.baeldung.jsoup;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import org.apache.commons.io.FileUtils;
6+
import org.jsoup.HttpStatusException;
7+
import org.jsoup.Jsoup;
8+
import org.jsoup.nodes.Document;
9+
import org.jsoup.nodes.Element;
10+
import org.jsoup.parser.Tag;
11+
import org.jsoup.select.Elements;
12+
13+
public class JsoupExample {
14+
15+
public static void main(String[] args) throws IOException {
16+
scrapeSpringBlog();
17+
}
18+
19+
static void scrapeSpringBlog() throws IOException {
20+
String blogUrl = "https://spring.io/blog";
21+
Document doc = Jsoup.connect(blogUrl).get();
22+
23+
try {
24+
Document doc404 = Jsoup.connect("https://spring.io/will-not-be-found").get();
25+
} catch (HttpStatusException ex) {
26+
System.out.println(ex.getMessage());
27+
}
28+
29+
Document docCustomConn = Jsoup.connect(blogUrl).userAgent("Mozilla").get();
30+
docCustomConn = Jsoup.connect(blogUrl).timeout(5000).get();
31+
docCustomConn = Jsoup.connect(blogUrl).cookie("cookiename", "val234").get();
32+
// docCustomConn = Jsoup.connect(blogUrl).data("datakey", "datavalue").post();
33+
docCustomConn = Jsoup.connect(blogUrl).header("headersecurity", "xyz123").get();
34+
35+
docCustomConn = Jsoup.connect(blogUrl)
36+
.userAgent("Mozilla")
37+
.timeout(5000)
38+
.cookie("cookiename", "val234")
39+
.cookie("anothercookie", "ilovejsoup")
40+
.header("headersecurity", "xyz123")
41+
.get();
42+
43+
Elements links = doc.select("a");
44+
Elements sections = doc.select("section");
45+
Elements logo = doc.select(".spring-logo--container");
46+
Elements pagination = doc.select("#pagination_control");
47+
Elements divsDescendant = doc.select("header div");
48+
Elements divsDirect = doc.select("header > div");
49+
50+
Element pag = doc.getElementById("pagination_control");
51+
Elements desktopOnly = doc.getElementsByClass("desktopOnly");
52+
53+
Element firstSection = sections.first();
54+
Element lastSection = sections.last();
55+
Element secondSection = sections.get(2);
56+
Elements allParents = firstSection.parents();
57+
Element parent = firstSection.parent();
58+
Elements children = firstSection.children();
59+
Elements siblings = firstSection.siblingElements();
60+
61+
sections.stream().forEach(el -> System.out.println("section: " + el));
62+
63+
Elements sectionParagraphs = firstSection.select(".paragraph");
64+
65+
Element firstArticle = doc.select("article").first();
66+
Element timeElement = firstArticle.select("time").first();
67+
String dateTimeOfFirstArticle = timeElement.attr("datetime");
68+
Element sectionDiv = firstArticle.select("section div").first();
69+
String sectionDivText = sectionDiv.text();
70+
String articleHtml = firstArticle.html();
71+
String outerHtml = firstArticle.outerHtml();
72+
73+
timeElement.attr("datetime", "2016-12-16 15:19:54.3");
74+
sectionDiv.text("foo bar");
75+
firstArticle.select("h2").html("<div><span></span></div>");
76+
77+
Element link = new Element(Tag.valueOf("a"), "")
78+
.text("Checkout this amazing website!")
79+
.attr("href", "http://baeldung.com")
80+
.attr("target", "_blank");
81+
firstArticle.appendChild(link);
82+
83+
doc.select("li.navbar-link").remove();
84+
firstArticle.select("img").remove();
85+
86+
File indexFile = new File("/tmp", "spring_blog_home.html");
87+
FileUtils.writeStringToFile(indexFile, doc.html(), doc.charset());
88+
}
89+
}

‎pom.xml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.baeldung</groupId>
66
<artifactId>parent-modules</artifactId>
@@ -64,11 +64,12 @@
6464
<module>jsf</module>
6565
<module>json-path</module>
6666
<module>json</module>
67+
<module>jsoup</module>
6768
<module>junit5</module>
6869

6970
<module>log-mdc</module>
7071
<module>log4j</module>
71-
<module>log4j2</module>
72+
<module>log4j2</module>
7273
<module>lombok</module>
7374

7475
<module>mapstruct</module>
@@ -119,8 +120,8 @@
119120
<module>spring-jpa</module>
120121
<module>spring-katharsis</module>
121122
<module>spring-mockito</module>
122-
<module>spring-mvc-email</module>
123-
<module>spring-mvc-forms</module>
123+
<module>spring-mvc-email</module>
124+
<module>spring-mvc-forms</module>
124125
<module>spring-mvc-java</module>
125126
<module>spring-mvc-no-xml</module>
126127
<module>spring-mvc-tiles</module>

0 commit comments

Comments
 (0)