0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Amazonで本のタイトルとISBNを取得する(スクレイピング)

Posted at

はじめに

欲しい本をAmazonのお気に入りにストックしてあるとします。
これらのタイトルとISBNを抜き出すのに、少しでも楽できるように、processingでプログラムを作りました。

環境

Processing 4
MacOSX
jsoupライブラリ

※以下からjarをダウンロードし、processingプログラムのフォルダの下にcodeという名前のフォルダを作り、その中にjarファイルを置く

image.png

プログラム

プログラムの中に、URLをベタ書きです。

getTitleISBNatAmazon.pde
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

void setup() {
  String [] urls = {
    "https://amzn.asia/d/hyyRyIR",
    "https://amzn.asia/d/7gktE7f",
    "https://amzn.asia/d/jfqVRj7"
  };

  for (String url : urls) {
    try {
      Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0").get();

      // タイトルを取得
      Element titleElement = doc.getElementById("productTitle");
      String title = titleElement.text();
      println("Title: " + title);

      // ISBNを取得
      Elements listItems = doc.select("ul li");
      String isbn = "";
      for (Element li : listItems) {
        if (li.text().contains("ISBN-13")) {
          isbn = li.text().split(":")[1].trim();
          break;
        }
      }
      if (!isbn.isEmpty()) {
        println("ISBN: " + isbn);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

出力例

Title: 生成AIのしくみ 〈流れ〉が画像・音声・動画をつくる (岩波科学ライブラリー 328)
ISBN: ‎ 978-4000297288
Title: ローカルLLM実践入門
ISBN: ‎ 978-4296206728
Title: 数学の美: 情報を支える数理の世界
ISBN: ‎ 978-4807920525

※とくに、選書の意図はありません。偶然、一番に表示されたものです。

補足

かなりchatGTPに書いてもらいました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?