Jsoup – An Introduction with example

Published on Author bloggerthreeLeave a comment

What is Jsoup ?

Jsoup is a open source  Java library used to deal with real-world HTML.

It can parse  HTML from a URL, file, or string.

It gives a very useful API for finding, extracting and manipulating data,

using DOM traversal or  CSS selectors.

It also supports jquery-like methods.


How to use Jsoup ?

At first download the Jsoup jar file from http://jsoup.org/download

Import that external Jsoup jar file using  eclipse or others IDE.

Example

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.select.Elements;
public class ParseHtml
//Aim : Fetch the image au, that ends with gAt from given URL
public static void main(String[] args) throws java.io.I0Exception
try

{
// Connect to the URL

Document parseDocument = Jscup.connect(“http://pastebin.com/raw.php?i=rKDnksMd”).get();
// Select the Diy tag that has ImageDiv class

Elements imgDiv = parseDocument.select(“div.ImageDiv”);

// Select the Image Tag that image pr.. ends with gif

Elements imgTag = imgDiv.select(“img[src$=.gif]”);
// Select Src of the Image Tag

String imgSrc = imgTag.attr(“src”);
// Print the output

System.out.println(imgSrc);

}

catch (Exception e)

{

System.out.println(e.toString())

}

}

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *