This is a sample program to read title and description of specified url.
Wh have used java html parser jsoup for this program.
for that you have to include jsoup-1.7.2.jar(used in this program).
First we have to connect to url using
Document doc Jsoup.connect(url).get();
To get title call title() that will return String value
String title=doc.title();
To select meta with description and attribute content we use following code
String description = doc.select(“meta[name=description]”).get(0).attr(“content”);
To select first description we use following snippet
Element meta = doc.select(“meta[name=description]”).first();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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 jsop { public static void main(String[] args) { Document doc; try { // need http protocol doc = Jsoup.connect("http://yahoo.com").get(); // get page title String title = doc.title(); System.out.println("title : " + title); String description = doc.select("meta[name=description]").get(0).attr("content"); System.out.println("Meta description 1 : " + description); //this will also fetch the description Element meta = doc.select("meta[name=description]").first(); System.out.println("Meta description 2 :" +meta.attr("content")); } catch (Exception e) { System.out.println("error : " + e); } } } |
Result
1 2 3 | title : Yahoo Meta description 1 : News, email and search are just the beginning. Discover more every day. Find your yodel. Meta description 2 :News, email and search are just the beginning. Discover more every day. Find your yodel. |