HtmlCleanerでOPG情報を取得

HtmlCleanerで、OPG情報を取得する例。 (以下では title と image を出力しているだけです)


import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.net.URI; import org.htmlcleaner.CleanerProperties; import org.htmlcleaner.HtmlCleaner; import org.htmlcleaner.PrettyXmlSerializer; import org.htmlcleaner.TagNode; public class Test { public static void main(String[] args) throws MalformedURLException { CleanerProperties props = new CleanerProperties(); final SimpleHtmlSerializer htmlSerializer = new SimpleHtmlSerializer(props); TagNode node = null; try { node = new HtmlCleaner(props).clean( new URL("https://hacknote.jp/") ); TagNode[] metaElements = node.getElementsByName("meta", true); for(int i = 0; i < metaElements.length; i++){ TagNode metaElement = metaElements[i]; String property = metaElement.getAttributeByName("property"); if("og:title".equals(property)){ System.out.println(metaElement.getAttributeByName("content")); }else if("og:image".equals(property)){ System.out.println(metaElement.getAttributeByName("content")); } } } catch(IOException e){ System.out.println(e.getMessage()); } catch (Exception ex){ System.out.println(ex.getMessage()); } } }