|
| 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 | +} |
0 commit comments