11package com .baeldung .jsoup ;
22
3- import java .io .File ;
43import java .io .IOException ;
5- import org .apache .commons .io .FileUtils ;
6- import org .jsoup .HttpStatusException ;
74import org .jsoup .Jsoup ;
85import org .jsoup .nodes .Document ;
96import org .jsoup .nodes .Element ;
107import org .jsoup .parser .Tag ;
118import org .jsoup .select .Elements ;
129
13- public class JsoupExample {
10+ public class JsoupParser {
1411
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- }
12+ Document doc ;
2813
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 ();
14+ public void loadDocument (String blogUrl ) throws IOException {
15+ doc = Jsoup .connect (blogUrl ).get ();
16+ }
3417
35- docCustomConn = Jsoup .connect (blogUrl )
18+ void loadDocumentCustomized (String blogUrl ) throws IOException {
19+ doc = Jsoup .connect (blogUrl )
3620 .userAgent ("Mozilla" )
3721 .timeout (5000 )
3822 .cookie ("cookiename" , "val234" )
3923 .cookie ("anothercookie" , "ilovejsoup" )
24+ .referrer ("http://google.com" )
4025 .header ("headersecurity" , "xyz123" )
4126 .get ();
27+ }
4228
29+ void examplesSelectors () {
4330 Elements links = doc .select ("a" );
44- Elements sections = doc .select ("section" );
4531 Elements logo = doc .select (".spring-logo--container" );
4632 Elements pagination = doc .select ("#pagination_control" );
4733 Elements divsDescendant = doc .select ("header div" );
@@ -50,6 +36,14 @@ static void scrapeSpringBlog() throws IOException {
5036 Element pag = doc .getElementById ("pagination_control" );
5137 Elements desktopOnly = doc .getElementsByClass ("desktopOnly" );
5238
39+ Elements sections = doc .select ("section" );
40+ Element firstSection = sections .first ();
41+ Elements sectionParagraphs = firstSection .select (".paragraph" );
42+ }
43+
44+ void examplesTraversing () {
45+ Elements sections = doc .select ("section" );
46+
5347 Element firstSection = sections .first ();
5448 Element lastSection = sections .last ();
5549 Element secondSection = sections .get (2 );
@@ -59,17 +53,24 @@ static void scrapeSpringBlog() throws IOException {
5953 Elements siblings = firstSection .siblingElements ();
6054
6155 sections .stream ().forEach (el -> System .out .println ("section: " + el ));
56+ }
6257
63- Elements sectionParagraphs = firstSection .select (".paragraph" );
64-
58+ void examplesExtracting () {
6559 Element firstArticle = doc .select ("article" ).first ();
6660 Element timeElement = firstArticle .select ("time" ).first ();
6761 String dateTimeOfFirstArticle = timeElement .attr ("datetime" );
6862 Element sectionDiv = firstArticle .select ("section div" ).first ();
6963 String sectionDivText = sectionDiv .text ();
7064 String articleHtml = firstArticle .html ();
7165 String outerHtml = firstArticle .outerHtml ();
66+ }
7267
68+ void examplesModifying () {
69+ Element firstArticle = doc .select ("article" ).first ();
70+ Element timeElement = firstArticle .select ("time" ).first ();
71+ Element sectionDiv = firstArticle .select ("section div" ).first ();
72+
73+ String dateTimeOfFirstArticle = timeElement .attr ("datetime" );
7374 timeElement .attr ("datetime" , "2016-12-16 15:19:54.3" );
7475 sectionDiv .text ("foo bar" );
7576 firstArticle .select ("h2" ).html ("<div><span></span></div>" );
@@ -82,8 +83,9 @@ static void scrapeSpringBlog() throws IOException {
8283
8384 doc .select ("li.navbar-link" ).remove ();
8485 firstArticle .select ("img" ).remove ();
86+ }
8587
86- File indexFile = new File ( "/tmp" , "spring_blog_home.html" );
87- FileUtils . writeStringToFile ( indexFile , doc .html (), doc . charset () );
88+ String getTidyHtml () {
89+ return doc .html ();
8890 }
8991}
0 commit comments