SlideShare a Scribd company logo
1 of 19
http://www.javatpoint.com/jsoup-tutorial
Introduction to Jsoup Tutorial
➢ Jsoup is a java html parser.
➢ It is a java library that to parse html document.
➢ Jsoup is uses DOM, CSS and Jquery-like method for extracting and
manipulating file.
How to install Jsoup?
If you want to run Jsoup query it is necessary to install jsoup.
There are two way to install jsoup:-
1. By maven pom.xml
2. By jsoup.jar file
Install by Maven pom.xml
To install jsoup using maven:-
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.1</version>
</dependacy>
Install by jsoup.jar file
To download jsoup.jar file:-
1. Click here to ======>>> download jsoup.jar file
2. to set the classpath of jsoup.jar file.
3. write the following command on console.
set classpath=jsoup-1.8.1.jar;.;%classpath%
Jsoup Example
There are given a lot jsoup examples as follow:
1. Get Title of URL
2. Get Title from HTML file
3. Get Total link of URL
4. Get meta information of URL
5. Get Total images of URL
6. Get Form parameters
Jsoup Example: print title of an url
Let’s see the example of print title of an url given below:
1. import java.io.IOException;
2. import org.jsoup.Jsoup;
3. import org.jsoup.nodes.Document;
4. public class FirstJsoupExample{
5. public static void main( String[] args ) throws IOException{
6. Document doc = Jsoup.connect("http://www.javatpoint.com").get();
7. String title = doc.title();
8. System.out.println("title is: " + title);
9. }
10. }
Output:
title is : Javatpoint- A solution of all Technology
Jsoup Example: get title from html file
Let’s see the example of get jsoup title from html file as given below:
1. import java.io.File;
2. import java.io.IOException;
3. import org.jsoup.Jsoup;
4. import org.jsoup.nodes.Document;
5. public class JsoupPrintTitlefromHtml{
6. public static void main( String[] args ) throws IOException{
7. Document doc = Jsoup.parse(new File("e:register.html"),"utf-8");
8. String title = doc.title();
9. System.out.println("title is: " + title);
10. }
11. }
Output: title is: Please Register
Jsoup Example: get the link of an url
1. import java.io.IOException;
2. import org.jsoup.Jsoup;
3. import org.jsoup.nodes.Document;
4. import org.jsoup.nodes.Element;
5. import org.jsoup.select.Elements;
6. public class JsoupPrintLinks {
7. public static void main( String[] args ) throws IOException{
8. Document doc = Jsoup.connect("http://www.javatpoint.com").get();
9. Elements links = doc.select("a[href]");
10. for (Element link : links) {
11. System.out.println("nlink : " + link.attr("href"));
12. System.out.println("text : " + link.text());
13. }
14. }
15. }
Output: get links of an url
output:-
link : http://www.javatpoint.com/contribute-us
text : Contribute Us
link : http://www.javatpoint.com/asknewquestion.jsp
text : Ask Question
link : http://www.javatpoint.com/login.jsp
text : login
.....
Jsoup Example: get the meta data of url
Let see the example of get meta data of url:
1. import java.io.IOException;
2. import org.jsoup.Jsoup;
3. import org.jsoup.nodes.Document;
4. public class JsoupPrintMetadata {
5. public static void main( String[] args ) throws IOException{
6. Document doc = Jsoup.connect("http://www.javatpoint.com").get();
7.
8. String keywords = doc.select("meta[name=keywords]").first().attr("content");
9. System.out.println("Meta keyword : " + keywords);
10. String description = doc.select("meta[name=description]").get(0).attr("content");
11. System.out.println("Meta description : " + description);
12. }
13. }
Output: get meta data of url
Output:-
Meta keyword : jsoup, tutorial, beginners, professionals, introduction, example,
java, html, parser
Meta description : Jsoup tutorial for beginners and professionals provides html
parsing facility
in java with examples of printing title, links, images, form elements from url.
jsoup Example: get image of url
Example of get image url-
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.javatpoint.com").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: get image url
src : http://www.javatpoint.com/images/social/r.png
height :
width :
alt : RSS Feed
src : http://www.javatpoint.com/images/social/m.png
height :
width :
alt : Subscribe to Get Email Alerts
src : http://www.javatpoint.com/images/social/f.png
height :
width :
alt : Facebook Page
src : http://www.javatpoint.com/images/social/g.png
height :
width :
alt : Google Page
Jsoup Example: print from parameters
<!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>
JsoupPrintFormParameters.java
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
Contact us:
Resources:
Visit- www.javatpoint.com
vist us on facebook- www.facebook.com/javatpoint

More Related Content

What's hot

Appengine Java Night #2a
Appengine Java Night #2aAppengine Java Night #2a
Appengine Java Night #2a
Shinichi Ogawa
 
Creating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDBCreating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDB
Wildan Maulana
 
Asp.net create delete directory folder in c# vb.net
Asp.net   create delete directory folder in c# vb.netAsp.net   create delete directory folder in c# vb.net
Asp.net create delete directory folder in c# vb.net
relekarsushant
 
Appengine Java Night #2b
Appengine Java Night #2bAppengine Java Night #2b
Appengine Java Night #2b
Shinichi Ogawa
 
Lucene for Solr Developers
Lucene for Solr DevelopersLucene for Solr Developers
Lucene for Solr Developers
Erik Hatcher
 

What's hot (20)

Appengine Java Night #2a
Appengine Java Night #2aAppengine Java Night #2a
Appengine Java Night #2a
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutions
 
Building your own search engine with Apache Solr
Building your own search engine with Apache SolrBuilding your own search engine with Apache Solr
Building your own search engine with Apache Solr
 
Getting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETGetting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NET
 
Creating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDBCreating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDB
 
Asp.net create delete directory folder in c# vb.net
Asp.net   create delete directory folder in c# vb.netAsp.net   create delete directory folder in c# vb.net
Asp.net create delete directory folder in c# vb.net
 
Lucene
LuceneLucene
Lucene
 
Jsp
JspJsp
Jsp
 
Appengine Java Night #2b
Appengine Java Night #2bAppengine Java Night #2b
Appengine Java Night #2b
 
Lucene for Solr Developers
Lucene for Solr DevelopersLucene for Solr Developers
Lucene for Solr Developers
 
it's just search
it's just searchit's just search
it's just search
 
Tutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginTutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component plugin
 
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardAndroid | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
 
Apache Solr Workshop
Apache Solr WorkshopApache Solr Workshop
Apache Solr Workshop
 
Using Apache Solr
Using Apache SolrUsing Apache Solr
Using Apache Solr
 
Enterprise Search Solution: Apache SOLR. What's available and why it's so cool
Enterprise Search Solution: Apache SOLR. What's available and why it's so coolEnterprise Search Solution: Apache SOLR. What's available and why it's so cool
Enterprise Search Solution: Apache SOLR. What's available and why it's so cool
 
Android Data Persistence
Android Data PersistenceAndroid Data Persistence
Android Data Persistence
 
Building a Testable Data Access Layer
Building a Testable Data Access LayerBuilding a Testable Data Access Layer
Building a Testable Data Access Layer
 
Discovering Django - zekeLabs
Discovering Django - zekeLabsDiscovering Django - zekeLabs
Discovering Django - zekeLabs
 
Using Fuzzy Code Search to Link Code Fragments in Discussions to Source Code
Using Fuzzy Code Search to Link Code Fragments in Discussions to Source CodeUsing Fuzzy Code Search to Link Code Fragments in Discussions to Source Code
Using Fuzzy Code Search to Link Code Fragments in Discussions to Source Code
 

Viewers also liked

Crawleando a web feito gente grande com o scrapy
Crawleando a web feito gente grande com o scrapyCrawleando a web feito gente grande com o scrapy
Crawleando a web feito gente grande com o scrapy
Bernardo Fontes
 

Viewers also liked (6)

CCNA Interview Questions and Answer ppt - JavaTpoint
CCNA Interview Questions and Answer ppt - JavaTpointCCNA Interview Questions and Answer ppt - JavaTpoint
CCNA Interview Questions and Answer ppt - JavaTpoint
 
Web Crawling
Web CrawlingWeb Crawling
Web Crawling
 
Web crawler
Web crawlerWeb crawler
Web crawler
 
Parsing XML Data
Parsing XML DataParsing XML Data
Parsing XML Data
 
Current challenges in web crawling
Current challenges in web crawlingCurrent challenges in web crawling
Current challenges in web crawling
 
Crawleando a web feito gente grande com o scrapy
Crawleando a web feito gente grande com o scrapyCrawleando a web feito gente grande com o scrapy
Crawleando a web feito gente grande com o scrapy
 

Similar to Jsoup Tutorial for Beginners - Javatpoint

Step 8_7_ 6_5_4_3_2_ 1 in one_Tutorial for Begineer on Selenium Web Driver-Te...
Step 8_7_ 6_5_4_3_2_ 1 in one_Tutorial for Begineer on Selenium Web Driver-Te...Step 8_7_ 6_5_4_3_2_ 1 in one_Tutorial for Begineer on Selenium Web Driver-Te...
Step 8_7_ 6_5_4_3_2_ 1 in one_Tutorial for Begineer on Selenium Web Driver-Te...
Rashedul Islam
 
Building a Driver: Lessons Learned From Developing the Internet Explorer Driver
Building a Driver: Lessons Learned From Developing the Internet Explorer DriverBuilding a Driver: Lessons Learned From Developing the Internet Explorer Driver
Building a Driver: Lessons Learned From Developing the Internet Explorer Driver
seleniumconf
 
070517 Jena
070517 Jena070517 Jena
070517 Jena
yuhana
 
cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123
Parag Gajbhiye
 

Similar to Jsoup Tutorial for Beginners - Javatpoint (20)

[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
 
Play framework
Play frameworkPlay framework
Play framework
 
Step 8_7_ 6_5_4_3_2_ 1 in one_Tutorial for Begineer on Selenium Web Driver-Te...
Step 8_7_ 6_5_4_3_2_ 1 in one_Tutorial for Begineer on Selenium Web Driver-Te...Step 8_7_ 6_5_4_3_2_ 1 in one_Tutorial for Begineer on Selenium Web Driver-Te...
Step 8_7_ 6_5_4_3_2_ 1 in one_Tutorial for Begineer on Selenium Web Driver-Te...
 
Using Twig with Drupal 7
Using Twig with Drupal 7Using Twig with Drupal 7
Using Twig with Drupal 7
 
Spring hibernate jsf_primefaces_intergration
Spring hibernate jsf_primefaces_intergrationSpring hibernate jsf_primefaces_intergration
Spring hibernate jsf_primefaces_intergration
 
Building a Dynamic Website Using Django
Building a Dynamic Website Using DjangoBuilding a Dynamic Website Using Django
Building a Dynamic Website Using Django
 
Bootstrap and XPages (DanNotes 2013)
Bootstrap and XPages (DanNotes 2013)Bootstrap and XPages (DanNotes 2013)
Bootstrap and XPages (DanNotes 2013)
 
Joomla! Components - Uma visão geral
Joomla! Components - Uma visão geralJoomla! Components - Uma visão geral
Joomla! Components - Uma visão geral
 
Android ui layouts ,cntls,webservices examples codes
Android ui layouts ,cntls,webservices examples codesAndroid ui layouts ,cntls,webservices examples codes
Android ui layouts ,cntls,webservices examples codes
 
Image Converter
Image ConverterImage Converter
Image Converter
 
Tomcat + other things
Tomcat + other thingsTomcat + other things
Tomcat + other things
 
What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.
 
Building a Driver: Lessons Learned From Developing the Internet Explorer Driver
Building a Driver: Lessons Learned From Developing the Internet Explorer DriverBuilding a Driver: Lessons Learned From Developing the Internet Explorer Driver
Building a Driver: Lessons Learned From Developing the Internet Explorer Driver
 
ExtraFileIO.pptx
ExtraFileIO.pptxExtraFileIO.pptx
ExtraFileIO.pptx
 
Building High Performance Web Applications and Sites
Building High Performance Web Applications and SitesBuilding High Performance Web Applications and Sites
Building High Performance Web Applications and Sites
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
070517 Jena
070517 Jena070517 Jena
070517 Jena
 
Xml & Java
Xml & JavaXml & Java
Xml & Java
 
cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123
 

More from JavaTpoint.Com

More from JavaTpoint.Com (8)

5 Reason, Why Python Popular.pdf
5 Reason, Why Python Popular.pdf5 Reason, Why Python Popular.pdf
5 Reason, Why Python Popular.pdf
 
History and Versions of Java Programming.pdf
History and Versions of Java Programming.pdfHistory and Versions of Java Programming.pdf
History and Versions of Java Programming.pdf
 
4 Network Certifications for Your IT Career in 2022.pdf
4 Network Certifications for Your IT Career in 2022.pdf4 Network Certifications for Your IT Career in 2022.pdf
4 Network Certifications for Your IT Career in 2022.pdf
 
Skills required for an IoT Developer.pdf
Skills required for an IoT Developer.pdfSkills required for an IoT Developer.pdf
Skills required for an IoT Developer.pdf
 
Cloud computing tutorial for beginners
Cloud computing tutorial for beginnersCloud computing tutorial for beginners
Cloud computing tutorial for beginners
 
Get an Android tutorial for beginners
Get an Android tutorial for beginnersGet an Android tutorial for beginners
Get an Android tutorial for beginners
 
What is Ajax technology?
What is Ajax technology?What is Ajax technology?
What is Ajax technology?
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Jsoup Tutorial for Beginners - Javatpoint

  • 2. Introduction to Jsoup Tutorial ➢ Jsoup is a java html parser. ➢ It is a java library that to parse html document. ➢ Jsoup is uses DOM, CSS and Jquery-like method for extracting and manipulating file.
  • 3. How to install Jsoup? If you want to run Jsoup query it is necessary to install jsoup. There are two way to install jsoup:- 1. By maven pom.xml 2. By jsoup.jar file
  • 4. Install by Maven pom.xml To install jsoup using maven:- <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.8.1</version> </dependacy>
  • 5. Install by jsoup.jar file To download jsoup.jar file:- 1. Click here to ======>>> download jsoup.jar file 2. to set the classpath of jsoup.jar file. 3. write the following command on console. set classpath=jsoup-1.8.1.jar;.;%classpath%
  • 6. Jsoup Example There are given a lot jsoup examples as follow: 1. Get Title of URL 2. Get Title from HTML file 3. Get Total link of URL 4. Get meta information of URL 5. Get Total images of URL 6. Get Form parameters
  • 7. Jsoup Example: print title of an url Let’s see the example of print title of an url given below: 1. import java.io.IOException; 2. import org.jsoup.Jsoup; 3. import org.jsoup.nodes.Document; 4. public class FirstJsoupExample{ 5. public static void main( String[] args ) throws IOException{ 6. Document doc = Jsoup.connect("http://www.javatpoint.com").get(); 7. String title = doc.title(); 8. System.out.println("title is: " + title); 9. } 10. } Output: title is : Javatpoint- A solution of all Technology
  • 8. Jsoup Example: get title from html file Let’s see the example of get jsoup title from html file as given below: 1. import java.io.File; 2. import java.io.IOException; 3. import org.jsoup.Jsoup; 4. import org.jsoup.nodes.Document; 5. public class JsoupPrintTitlefromHtml{ 6. public static void main( String[] args ) throws IOException{ 7. Document doc = Jsoup.parse(new File("e:register.html"),"utf-8"); 8. String title = doc.title(); 9. System.out.println("title is: " + title); 10. } 11. } Output: title is: Please Register
  • 9. Jsoup Example: get the link of an url 1. import java.io.IOException; 2. import org.jsoup.Jsoup; 3. import org.jsoup.nodes.Document; 4. import org.jsoup.nodes.Element; 5. import org.jsoup.select.Elements; 6. public class JsoupPrintLinks { 7. public static void main( String[] args ) throws IOException{ 8. Document doc = Jsoup.connect("http://www.javatpoint.com").get(); 9. Elements links = doc.select("a[href]"); 10. for (Element link : links) { 11. System.out.println("nlink : " + link.attr("href")); 12. System.out.println("text : " + link.text()); 13. } 14. } 15. }
  • 10. Output: get links of an url output:- link : http://www.javatpoint.com/contribute-us text : Contribute Us link : http://www.javatpoint.com/asknewquestion.jsp text : Ask Question link : http://www.javatpoint.com/login.jsp text : login .....
  • 11. Jsoup Example: get the meta data of url Let see the example of get meta data of url: 1. import java.io.IOException; 2. import org.jsoup.Jsoup; 3. import org.jsoup.nodes.Document; 4. public class JsoupPrintMetadata { 5. public static void main( String[] args ) throws IOException{ 6. Document doc = Jsoup.connect("http://www.javatpoint.com").get(); 7. 8. String keywords = doc.select("meta[name=keywords]").first().attr("content"); 9. System.out.println("Meta keyword : " + keywords); 10. String description = doc.select("meta[name=description]").get(0).attr("content"); 11. System.out.println("Meta description : " + description); 12. } 13. }
  • 12. Output: get meta data of url Output:- Meta keyword : jsoup, tutorial, beginners, professionals, introduction, example, java, html, parser Meta description : Jsoup tutorial for beginners and professionals provides html parsing facility in java with examples of printing title, links, images, form elements from url.
  • 13. jsoup Example: get image of url Example of get image url- 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.javatpoint.com").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")); } } }
  • 14. Output: get image url src : http://www.javatpoint.com/images/social/r.png height : width : alt : RSS Feed src : http://www.javatpoint.com/images/social/m.png height : width : alt : Subscribe to Get Email Alerts src : http://www.javatpoint.com/images/social/f.png height : width : alt : Facebook Page src : http://www.javatpoint.com/images/social/g.png height : width : alt : Google Page
  • 15. Jsoup Example: print from parameters <!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>
  • 16. JsoupPrintFormParameters.java 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); } } }
  • 17. 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
  • 19. Resources: Visit- www.javatpoint.com vist us on facebook- www.facebook.com/javatpoint