The Java implementation gets the keywords description of the website

  • 2020-04-01 03:44:17
  • OfStack

Access to websites < Meta name="keywords" content="" / > < Meta name = "description" content = "" > Keywords and description content

Implement the HTML parser jsoup

Download jsoup lib address: (link: http://jsoup.org/download)


package cn.evan.util;
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 SemanticCrawl {
    public static void main(String[] args) {
        Document doc = null;
        try {
            doc = Jsoup.connect(" The url ").get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String title = doc.title();
        Elements metas = doc.head().select("meta"); 
        for (Element meta : metas) { 
            String content = meta.attr("content"); 
            if ("keywords".equalsIgnoreCase(meta.attr("name"))) { 
                System.out.println(" Key words: "+content); 
            } 
            if ("description".equalsIgnoreCase(meta.attr("name"))) { 
                System.out.println(" Content description :"+content); 
            } 
        } 
        Elements keywords = doc.getElementsByTag("meta");
        System.out.println(" The title "+title);
    }
}

That's all I have to share in this article. I hope you enjoy it.


Related articles: