How to get page meta (title, description, images) like facebook attach url using Regex in java

前端 未结 4 1646
孤城傲影
孤城傲影 2021-02-19 08:35

How to get page meta (title, description, images) like facebook attach url using Regex in .java

4条回答
  •  忘了有多久
    2021-02-19 09:04

    I use JSOUP to get a Document object, then use something like the below method to get tags for each property I'm looking for.

    String findTag(Document document, String property) {
        String tag = null;
        String cssQuery = "meta[property='og:" + property + "']";
        Elements elements = document.select(cssQuery);
    
        if (elements != null && elements.size() >= 1) {
            tag = elements.first().attr("content");
        }
        return tag;
    }
    

    I used this often enough to where I decided to combine the fetching (with JSOUP) and parsing together into a library called ogmapper.

提交回复
热议问题