Jsoup は単純な情報をクロールします

1. Doban 書籍が最も注目を集めている

1.1 SpringBootプロジェクトまたはMavenプロジェクトの作成

1.2 jsoup の紹介

        <dependency>
            <!-- jsoup HTML parser library @ https://jsoup.org/ -->
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.15.3</version>
        </dependency>

バージョンは1.15.3である必要があります。1.15.4では情報が取得できない場合がありますのでご注意ください。

1.3 クロール情報

まず、Douban の書籍 URL を見つけます。
Douban

まず、レイアウト全体のうち、対応する要素タグを格納する部分 list-col2 を見つけます。
ここに画像の説明を挿入

したがって、ドキュメントの選択タグは ul.list-col2 li です。これは、ul 内のラベル list-col2 を選択し、その中の li を選択することを意味します。li は、表紙と情報を含む 2 つの部分に分かれています
。 infoの情報を取り出します
ここに画像の説明を挿入

[外部リンク画像の転送に失敗しました。ソース サイトにはリーチ防止メカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします (img-9GrSUwwo-1691985379249)(/images/pqtp-7.png)]

タイトルは h4.title の a タグで取得します。
著者は p.author で直接取得します。
内部のレーティングは 2 層で取得する必要があります。まず p タグを取得し、次に limian spam タグ p.entry を取得します。 -星-小さいスパン.平均-評価

public BaseResponse<String> famousDouBanBook(){
    
    
        String url = "https://book.douban.com/";
        Document document = null;
        try {
    
    
            document = Jsoup.connect(url).get();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        System.out.println(document);
        Elements elements = document.select("ul.list-col2 li");
        for (Element element : elements) {
    
    
            String title = element.select("h4.title a").text();
            String coverUrl = element.select("div.cover img").attr("src");
            String rate = element.select("p.entry-star-small span.average-rating").text();;
            String author = element.select("p.author").text();

            System.out.println("书名:" + title);
            System.out.println("封面:" + coverUrl);
            System.out.println("评分:" + rate);
            System.out.println("作者:" + author);
            System.out.println("------------------------");
        }
        return ResultUtils.success("ok");
    }

関連するエンティティ クラスを作成して情報を保存できます。

免責事項: 学習の参考のみを目的としています

おすすめ

転載: blog.csdn.net/qq_52843958/article/details/132273190