How to parse a website in java using jsoup ?

What is Jsoup ?

Jsoup is a java library, used to parse and extract content of a website.

As well as it can do many things, few things are given below,

Parsing any website content using get or post method,
Can able to parse any HTML elements,
Attributes and text etc…..

Download link:

http://jsoup.org/download

Parsing website (www.alexa.com) using jsoup:

Alexa.com is a rank provider website, that is they will give the rank for all the websites in the world based on the website traffic (global and regional wise).

Now we are going pass the website name and will get the rank details of the passed website using jsoup.

package in.javadomain;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class AlexaRank {
	public static void main(String[] args) throws IOException {
		String site = "www.javadomain.in";
		String siteRank = getAlexaRank("www.javadomain.in");
		System.out.println("Alexa Rank of " + site + ": \n" + siteRank);
	}

	public static String getAlexaRank(String siteName) throws IOException {
		String baseUrl = "http://www.alexa.com/siteinfo/" + siteName + "";
		String alexaRank = null;
		Document doc = Jsoup.connect(baseUrl).timeout(0).get();
		String globalRank = doc.select("span.globleRank")
				.select("strong.metricsUrl").select("strong.font-big2")
				.select("strong.valign").not("a").text();
		String countryRank = doc.select("span.countryRank")
				.select("strong.metricsUrl").select("strong.font-big2")
				.select("strong.valign").not("a").text();
		alexaRank = "Global Rank: " + globalRank + "\nCountry Rank: "
				+ countryRank;
		return alexaRank;
	}

}

 

Output:
Alexa Rank of www.javadomain.in:
Global Rank: 754,755
Country Rank: 235,150

Thanks for reading this post………..!!!

One comment on “How to parse a website in java using jsoup ?

  1. I loved aѕ much as уou’ll receive carried out right hегe.

    Тhе sketch іs attractive, your authored subject matter stylish.
    nοnetheless, yоu command get gоt an edginess over thаt
    you wish be delivering the follοwing. unwell unquestionably сome further fοrmerly again sіnce eхactly the
    same nearly veгy often inside case you shield thіs increase.

Leave a Reply

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

*

* Copy This Password *

* Type Or Paste Password Here *

99,254 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>