Jsoup Examples

Jsoup Examples tutorial for beginners and professionals, jsoup example using get title of url, get title from html, get total links of url, get meta information of url, get total images of url, get form parameters, file jsoup - java html parser providing facility to parse html document by java language with examples of printing title, links, images, form elements from url.

Jsoup Examples

Jsoup Examples

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

  1. Document doc = Jsoup.connect("http://www.javatpoint.com").get();  
  2. String title = doc.title(); 

Get title from HTML file

  1. Document doc = Jsoup.parse(new File("e:\\register.html"),"utf-8");//assuming register.html file in e drive  
  2. String title = doc.title(); 

Get total links of URL

  1. Document doc = Jsoup.connect("http://www.hpnmaratt.com").get();  
  2. Elements links = doc.select("a[href]");  
  3. for (Element link : links) {  
  4.     System.out.println("\nlink : " + link.attr("href"));  
  5.     System.out.println("text : " + link.text());  

Get meta information of URL

  1. Document doc = Jsoup.connect("http://www.astpoint.com").get();  
  2. String keywords = doc.select("meta[name=keywords]").first().attr("content");  
  3. System.out.println("Meta keyword : " + keywords);  
  4. String description = doc.select("meta[name=description]").get(0).attr("content");  
  5. System.out.println("Meta description : " + description); 

Get total images of URL

  1. Document doc = Jsoup.connect("http://www.satpoint.com").get();  
  2. Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]");  
  3. for (Element image : images) {  
  4.     System.out.println("src : " + image.attr("src"));  
  5.     System.out.println("height : " + image.attr("height"));  
  6.     System.out.println("width : " + image.attr("width"));  
  7.     System.out.println("alt : " + image.attr("alt"));  

Get form parameters

  1. Document doc = Jsoup.parse(new File("e:\\register.html"),"utf-8");  
  2. Element loginform = doc.getElementById("registerform");  
  3.   
  4. Elements inputElements = loginform.getElementsByTag("input");  
  5. for (Element inputElement : inputElements) {  
  6.     String key = inputElement.attr("name");  
  7.     String value = inputElement.attr("value");  
  8.     System.out.println("Param name: "+key+" \nParam value: "+value);  
  9. }