JavaとExcelを連携してスクレイピング画像を整理・出力する方法【Apache POI×Jsoup活用】

はじめに

Webスクレイピングで取得した画像を整理し、一覧としてExcelに出力できれば、報告書作成・調査記録・画像管理にとても便利です。

この記事では、以下のステップで画像整理システムを構築する方法を紹介します

  1. Javaで画像URLをスクレイピング
  2. 画像をローカルに保存
  3. Apache POIでExcelに一覧出力(サムネイル付き)

事前準備

使用ライブラリ

  • Jsoup:HTML解析(スクレイピング用)
  • Apache POI:Excelファイル出力
  • javax.imageio:画像読み込み・保存

Maven依存関係(pom.xml)

<dependencies>
  <dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.15.4</version>
  </dependency>
  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
  </dependency>
</dependencies>

1. スクレイピングで画像URLを取得

public List<String> extractImageUrls(String url) throws IOException {
    Document doc = Jsoup.connect(url).get();
    List<String> imageUrls = new ArrayList<>();
    for (Element img : doc.select("img[src]")) {
        imageUrls.add(img.absUrl("src"));
    }
    return imageUrls;
}

2. 画像をローカルに保存する

public void downloadImages(List<String> urls, String saveDir) throws IOException {
    for (int i = 0; i < urls.size(); i++) {
        URL url = new URL(urls.get(i));
        BufferedImage img = ImageIO.read(url);
        File output = new File(saveDir, "image_" + i + ".jpg");
        ImageIO.write(img, "jpg", output);
    }
}

3. Excelに画像一覧を出力(Apache POI)

public void exportToExcelWithImages(String folderPath, String excelPath) throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Image List");

    File[] images = new File(folderPath).listFiles((dir, name) -> name.endsWith(".jpg"));
    int rowIdx = 0;
    for (File image : images) {
        XSSFRow row = sheet.createRow(rowIdx++);
        row.createCell(0).setCellValue(image.getName());

        InputStream is = new FileInputStream(image);
        byte[] bytes = is.readAllBytes();
        int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
        is.close();

        CreationHelper helper = workbook.getCreationHelper();
        Drawing<?> drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(1);
        anchor.setRow1(rowIdx - 1);
        Picture pict = drawing.createPicture(anchor, pictureIdx);
        pict.resize(1.0);
    }

    try (FileOutputStream out = new FileOutputStream(excelPath)) {
        workbook.write(out);
    }
    workbook.close();
}

4. 実行例(mainメソッド)

public static void main(String[] args) throws Exception {
    String url = "https://example.com";
    String saveDir = "downloaded_images";
    String excelFile = "image_list.xlsx";

    new File(saveDir).mkdirs();

    ScraperExcelExporter exporter = new ScraperExcelExporter();
    List<String> imageUrls = exporter.extractImageUrls(url);
    exporter.downloadImages(imageUrls, saveDir);
    exporter.exportToExcelWithImages(saveDir, excelFile);

    System.out.println("画像の取得とExcel出力が完了しました。");
}

まとめ

この方法を使えば、Web上の画像を整理して、Excelファイルにサムネイル付きで一覧化することができます。報告書・リサーチ・社内共有など多くの業務で活用できる便利なテクニックです。

タイトルとURLをコピーしました