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:
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.
[java]
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.ngdeveloper.com";
String siteRank = getAlexaRank("www.ngdeveloper.com");
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;
}
}
[/java]
Output:
Alexa Rank of www.ngdeveloper.com:
Global Rank: 754,755
Country Rank: 235,150
Thanks for reading this post………..!!!