4
5

More than 5 years have passed since last update.

VineのURLからサムネイルを取得する

Posted at

取得自体はページをスクレイピングすることで行う。

流れは

  1. URLからDOMを引っ張ってくる
  2. metaタグからサムネイルのURLを抽出する

というふうになる。
Javaでやろうと思うとコードは以下の様な感じ。
(HTTP通信にはOkHttpを使用)


public class VineScraping
    private static final String REGEXP_THUMBNAIL = "property=\"og:image\" content=\"(.*?)\"";
    private static final Pattern PATTERN_THUMBNAIL = Pattern.compile(REGEXP_THUMBNAIL);

    public static void main(String[] args) {
        String url = "好きなURLを入れてね";
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        OkHttpClient client = new OkHttpClient();

        Response response = client.newCall(request).execute();
        String body = response.body().string();

        Matcher thumbnailMatcher = PATTERN_THUMBNAIL.matcher(body);
        Matcher movieMatcher = PATTERN_MOVIE.matcher(body);

        if (thumbnailMatcher.find()) {
            String thumbnailUrl = thumbnailMatcher.group(1);
            System.out.print(thumbnailUrl);
        }
    }
}

参考

4
5
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
4
5