Get title, meta description content using URL

后端 未结 1 1201
甜味超标
甜味超标 2020-12-19 16:11

I am trying to extract title and meta tag\'s description content from a URL, this is what I have:

fin[] //urls in a string array

for (int f = 0; f < fin.         


        
相关标签:
1条回答
  • 2020-12-19 16:51

    You can use this:

    String getMetaTag(Document document, String attr) {
        Elements elements = document.select("meta[name=" + attr + "]");
        for (Element element : elements) {
            final String s = element.attr("content");
            if (s != null) return s;
        }
        elements = document.select("meta[property=" + attr + "]");
        for (Element element : elements) {
            final String s = element.attr("content");
            if (s != null) return s;
        }
        return null;
    }
    

    Then:

     String title = document.title();
     String description = getMetaTag(document, "description");
     if (description == null) {
        description = getMetaTag(document, "og:description");
     }
     // and others you need to
     String ogImage = getMetaTag(document, "og:image") 
    

    ....

    0 讨论(0)
提交回复
热议问题