Java – How to get all links from a web page?

A jsoup HTML parser example to show you how to parse and get all HTML hyperlinks from a web page: pom.xml <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.12.1</version> </dependency> JsoupFindLinkSample.java package com.mkyong; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import java.util.HashSet; import java.util.Set; public class JsoupFindLinkSample { public static void main(String[] args) throws IOException { …

Read more

Java – Pretty Print HTML

In Java, we can use jsoup, a Java HTML parser, to parse a HTML code and pretty print it. pom.xml <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.12.1</version> </dependency> JavaPrettyPrintHTML.java package com.mkyong; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class JavaPrettyPrintHTML { public static void main(String[] args) { String html = "<html><body><h1>hello world</h1></body></html>"; System.out.println(html); // original Document doc = Jsoup.parse(html); // …

Read more

Jsoup – Check Redirect URL

In this article, we will show you how to use Jsoup to check if an URL is going to redirect. 1. URL Redirection Normally, a redirect URL will return an HTTP code of 301 or 307, and the target URL will be existed in the response header “location” field. Review a sample of HTTP Response …

Read more

jsoup : Send search query to Google

This example shows you how to use jsoup to send a search query to Google. Document doc = Jsoup .connect("https://www.google.com/search?q=mario"); .userAgent("Mozilla/5.0") .timeout(5000).get(); Unusual traffic from your computer network Don’t use this example to spam Google, you will get above message from Google, read this Google answer. 1. jsoup example Example to send a “mario” search …

Read more

Jsoup – Get favicon from html page

There are many ways the favicon can be recognized by the web browser : Example 1 <head> <link rel="icon" href="http://example.com/image.ico" /> </head> Example 2 <head> <link rel="icon" href="http://example.com/image.png" /> </head> Example 3 – weird, but Google use it. <head> <meta content="/images/google_favicon_128.png" itemprop="image" /> </head> 1. Jsoup Example Code snippets to get above favicon with Jsoup. …

Read more