In this guide we will discuss about Examples of Jsoup. There are given a lot of jsoup examples such as getting title, total links, total images and meta data of an URL or HTML document.
Get title of URL
- Document doc = Jsoup.connect(“http://www.Adglob.in”).get();
- String title = doc.title();
Let’s see the jsoup example to print title of an url e.g. www.Adglob.in. By the help of Jsoup.connect() method, we will connect with the URL. The get() method returns the reference of Document object. The document class provides title() method that returns the title of the document.
- import java.io.IOException;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- public class FirstJsoupExample{
- public static void main( String[] args ) throws IOException{
- Document doc = Jsoup.connect(“http://www.Adglob.in”).get();
- String title = doc.title();
- System.out.println(“title is: ” + title);
- }
- }
Output:
title is: Adglob - A Solution of all Technology
Get title from HTML file
- Document doc = Jsoup.parse(new File(“e:\\register.html”),”utf-8″);//assuming register.html file in e drive
- String title = doc.title();
In this example, we will get the title of the HTML page from the HTML file. To do so, we are going to call Jsoup.parse() method that returns the reference of Document. The title() method of Document class returns the title of the HTML document.
- import java.io.File;
- import java.io.IOException;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- public class JsoupPrintTitlefromHtml{
- public static void main( String[] args ) throws IOException{
- Document doc = Jsoup.parse(new File(“e:\\register.html”),”utf-8″);
- String title = doc.title();
- System.out.println(“title is: ” + title);
- }
- }
Output:
title is: Please Register
Get total links of URL
- Document doc = Jsoup.connect(“http://www.Adglob.in”).get();
- Elements links = doc.select(“a[href]”);
- for (Element link : links) {
- System.out.println(“\nlink : ” + link.attr(“href”));
- System.out.println(“text : ” + link.text());
- }
In this example, we will print the total links of an URL. To do so, we are going to call select() method of Document class that returns the reference of Elements. The Elements class have elements that can be traversed by for-each loop. The Element class provides attr() and text() methods to return link and text of the link.
- import java.io.IOException;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- public class JsoupPrintLinks {
- public static void main( String[] args ) throws IOException{
- Document doc = Jsoup.connect(“http://www.Adglob.in”).get();
- Elements links = doc.select(“a[href]”);
- for (Element link : links) {
- System.out.println(“\nlink : ” + link.attr(“href”));
- System.out.println(“text : ” + link.text());
- }
- }
- }
Output:
link : http://www.Adglob.in/contribute-us text : Contribute Us link : http://www.Adglob.in/asknewquestion.jsp text : Ask Question link : http://www.Adglob.in/login.jsp text : login .....
Get meta information of URL
- Document doc = Jsoup.connect(“http://www.Adglob.in”).get();
- String keywords = doc.select(“meta[name=keywords]”).first().attr(“content”);
- System.out.println(“Meta keyword : ” + keywords);
- String description = doc.select(“meta[name=description]”).get(0).attr(“content”);
- System.out.println(“Meta description : ” + description);
In this example, we will print the meta keywords and description of an URL. To do so, you need to call select(), first(), get() and attr() methods of Document class.
- import java.io.IOException;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- public class JsoupPrintMetadata {
- public static void main( String[] args ) throws IOException{
- Document doc = Jsoup.connect(“http://www.Adglob.in”).get();
- String keywords = doc.select(“meta[name=keywords]”).first().attr(“content”);
- System.out.println(“Meta keyword : ” + keywords);
- String description = doc.select(“meta[name=description]”).get(0).attr(“content”);
- System.out.println(“Meta description : ” + description);
- }
- }
Output:
Meta keyword : jsoup, chapter, beginners, professionals, introduction, example, java, html, parser Meta description : Jsoup chapter for beginners and professionals provides html parsing facility in java with examples of printing title, links, images, form elements from url.
Get total images of URL
- Document doc = Jsoup.connect(“http://www.Adglob.in”).get();
- Elements images = doc.select(“img[src~=(?i)\\.(png|jpe?g|gif)]”);
- for (Element image : images) {
- System.out.println(“src : ” + image.attr(“src”));
- System.out.println(“height : ” + image.attr(“height”));
- System.out.println(“width : ” + image.attr(“width”));
- System.out.println(“alt : ” + image.attr(“alt”));
- }
In this example, we will print the total images of an URL. To do so, we are calling select() method passing “img[src~=(?i)\\.(png|jpe?g|gif)]” as a parameter so that it can print png, jpeg or gif images.
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- public class JsoupPrintImages {
- public static void main( String[] args ) throws IOException{
- Document doc = Jsoup.connect(“http://www.Adglob.in”).get();
- Elements images = doc.select(“img[src~=(?i)\\.(png|jpe?g|gif)]”);
- for (Element image : images) {
- System.out.println(“src : ” + image.attr(“src”));
- System.out.println(“height : ” + image.attr(“height”));
- System.out.println(“width : ” + image.attr(“width”));
- System.out.println(“alt : ” + image.attr(“alt”));
- }
- }
- }
Output:
src : http://www.Adglob.in/images/social/r.png height : width : alt : RSS Feed src : http://www.Adglob.in /images/social/m.png height : width : alt : Subscribe to Get Email Alerts src : http://www.Adglob.in/images/social/f.png height : width : alt : Facebook Page src : http://www.Adglob.in/images/social/g.png height : width : alt : Google Page src : http://www.Adglob.in/images/social/t.png height : width : alt : Twitter Page src : images/logo/javahome.png height : width : alt : Java chapter src : images/logo/javascripthome.png height : width : alt : JavaScript chapter src : images/logo/sqlhome.png height : width : alt : SQL chapter src : images/logo/androidhome.png height : width : alt : Android chapter src : images/logo/clanguagehome.png height : width : alt : C Language chapter src : images/logo/html-chapter.png height : width : alt : html chapter src : images/logo/pythonhome.png height : width : alt : Python chapter src : images/logo/ajaxhome.png height : width : alt : AJAX chapter src : images/logo/cloudhome.png height : width : alt : Cloud chapter src : images/logo/javahome.png height : width : alt : Core Java chapter src : images/logo/javahome.png height : width : alt : Java Servlet chapter src : images/logo/jsphome.png height : width : alt : Java JSP chapter src : images/logo/javahome.png height : width : alt : EJB chapter src : images/logo/javahome.png height : width : alt : JAXB chapter src : images/logo/strutshome.png height : width : alt : Struts chapter src : images/logo/hibernatehome.png height : width : alt : Hibernate chapter src : images/logo/springhome.png height : width : alt : Spring chapter src : images/logo/javahome.png height : width : alt : Java Mail chapter src : images/logo/javahome.png height : width : alt : Java Design Pattern chapter src : images/logo/javahome.png height : width : alt : JUnit chapter src : images/logo/strutshome.png height : width : alt : Maven chapter src : images/logo/interviewhome.png height : width : alt : Interview Questions src : images/logo/projecthome.png height : width : alt : Free Projects src : images/logo/forumhome3.png height : width : alt : Forum chapter src : images/logo/quizhome.png height : width : alt : Online quiz src : images/logo/javacompiler.png height : width : alt : Online java compiler src : images/sonoo9.jpg height : width : alt : sonoo jaiswal src : http://www.Adglob.in/images/social/rss1.png height : width : alt : RSS Feed src : http://www.Adglob.in/images/social/mail1.png height : width : alt : Subscribe to Get Email Alerts src : http://www.Adglob.in/images/social/facebook1.jpg height : width : alt : Facebook Page src : http://www.Adglob.in/images/social/google1.png height : width : alt : Google Page src : http://www.Adglob.in/images/social/twitter1.png height : width : alt : Twitter Page src : http://www.Adglob.in/images/social/blog.png height : width : alt : Blog Page src : http://images.dmca.com/Badges/dmca_protected_sml_120c.png ?ID=e8b533d5-7356-47f5-820b-72c890f03a4e height : width : alt : DMCA.com
Get form parameters
- Document doc = Jsoup.parse(new File(“e:\\register.html”),”utf-8″);
- Element loginform = doc.getElementById(“registerform”);
- Elements inputElements = loginform.getElementsByTag(“input”);
- for (Element inputElement : inputElements) {
- String key = inputElement.attr(“name”);
- String value = inputElement.attr(“value”);
- System.out.println(“Param name: “+key+” \nParam value: “+value);
- }
In this example, we will print form parameters like parameter name and parameter value. To do so, we are calling getElementById() method of Document class and getElementsByTag() method of Element class.register.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset=”utf-8″>
- <title>Register Please</title>
- </head>
- <body>
- <form id=”registerform” action=”register.jsp” method=”post”>
- Name:<input type=”text” name=”name” value=”sonoo”/><br/>
- Password:<input type=”password” name=”password” value=”sj”/><br/>
- Email:<input type=”email” name=”email” value=”sonoojaiswal1987@gmail.com”/><br/>
- <input name=”submitbutton” type=”submit” value=”register”/>
- </form>
- </body>
- </html>
- import java.io.File;
- import java.io.IOException;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- public class JsoupPrintFormParameters {
- public static void main(String[] args) throws IOException {
- Document doc = Jsoup.parse(new File(“e:\\register.html”),”utf-8″);
- Element loginform = doc.getElementById(“registerform”);
- Elements inputElements = loginform.getElementsByTag(“input”);
- for (Element inputElement : inputElements) {
- String key = inputElement.attr(“name”);
- String value = inputElement.attr(“value”);
- System.out.println(“Param name: “+key+” \nParam value: “+value);
- }
- }
- }
Output:
Param name: name Param value: sonoo Param name: password Param value: sj Param name: email Param value: sonoojaiswal1987@gmail.com Param name: submitbutton Param value: register
Learn More : Click Here
I truly appreciate this post.Much thanks again. Really Cool.