LoginSignup
10
9

More than 5 years have passed since last update.

Javaで外部サイトのOGPメタ情報を取得する方法

Posted at

Javaを使用し外部URLからHTMLを取得して、簡単にパースしOGP情報を取得したい

スクレイピングにはJsoupを使用します。

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Ogp {
   public Elements getOgp(String url) throws IOException {
        Document document = Jsoup.connect(url).get();
        return document.select("meta[property~=og:*]");
    }
}

取得するだけならこれだけ

データもこんな感じで取り出せる

Elements elements = getOgp("http://gamy.jp");
for (Element element : elements) {
   System.out.println(element.attr("property"));
   System.out.println(element.attr("content"));
}
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9