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

前端 未结 4 1648
孤城傲影
孤城傲影 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:06

    How about this? Below statement parse all tags start with "og:". It's useful.

    doc.select("meta[property^=og:]")

    void parseOGTag(String response) {
        // Parse og tags
        Document doc = Jsoup.parse(response);
        Elements ogTags = doc.select("meta[property^=og:]");
        if (ogTags.size() <= 0) {
            return;
        }
    
        // Set OGTags you want
        String title;
        String desc;
        String image;
        for (int i = 0; i < ogTags.size(); i++) {
            Element tag = ogTags.get(i);
    
            String text = tag.attr("property");
            if ("og:image".equals(text)) {
                image = tag.attr("content");
            } else if ("og:description".equals(text)) {
                desc = tag.attr("content");
            } else if ("og:title".equals(text)) {
                title = tag.attr("content");
            }
        }                    
    }
    

提交回复
热议问题