java不能寫爬蟲嗎?海量美女圖片任你選!

https://gitee.com/gao_zhenzhong/spring-boots/tree/master/ok/98-app/15-boot-beautiful

1 主程序

package com.gzz;

import static com.gzz.utils.Utils.FORMAT;

import java.nio.file.Files;
import java.nio.file.Paths;

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

import com.gzz.utils.Utils;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 高振中
 * @date 2023-06-18 00:16:58
 **/
@Slf4j
public class Application18 {
    private static final String ROOT = "D:/[XIAOYU語畫界]/";
    private static final String FILE1 = ROOT + "1.txt";// 記錄前一頁編號
    private static final String FILE2 = ROOT + "2.txt";// 記錄前一圖集編號

    private static final String BASE = "https://mmm666.red/";
    private static final String SERIES = BASE + "tag/yuhuajie/";// 可換成其它系列

    @SneakyThrows
    public static void main(String[] args) {
        Utils.checkHistory(FILE1, FILE2);
        Integer frontPageNum = Integer.valueOf(Files.readString(Paths.get(FILE1)));// 上一完成頁編號
        log.info("已下線到第{}頁.....", frontPageNum);
        for (int i = frontPageNum; i < 53; i++) {
            Files.writeString(Paths.get(FILE1), i + "");
            Document curentPage = Jsoup.connect(SERIES + i).get();// 當(dāng)前下載頁
            Elements pageImageSet = curentPage.select("div.content div.row a");

            for (Element imageSet : pageImageSet) {
                String setName = imageSet.text().replace("[XIAOYU語畫界] ", "").replace("IAOYU語畫界]", "");

                int currentSetNum = Integer.valueOf(setName.substring(10).replaceAll("[^0-9]", ""));// 當(dāng)前圖集編號
                log.info("正在檢查\t第{}頁->圖集:\t{}", i, setName);
                Integer frontSetNum = Integer.valueOf(Files.readString(Paths.get(FILE2)));// 上一圖集編號
                if (currentSetNum <= frontSetNum) {
                    Files.writeString(Paths.get(FILE2), currentSetNum + "");
                    log.info("正在下載\t第{}頁->圖集:\t{}", i, setName);
                    Document imagesPage = Jsoup.connect(BASE + imageSet.attr("href")).get();
                    Elements images = imagesPage.select("#masonry div img");
                    int size = images.size();
                    images.remove(size - 1);
                    images.remove(size - 2);
                    for (int j = 0; j < images.size(); j++) {
                        String path = ROOT + setName + "/" + FORMAT.format(j) + ".webp";
                        String url = images.get(j).attr("data-original");
//                      System.out.println(url);
                        Utils.down(url, path);
                    }
                }
            }
        }
    }

}

2 工具類

package com.gzz;

import java.io.File;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DecimalFormat;

import lombok.SneakyThrows;

/**
 * @author 高振中
 * @date 2023-06-18 00:16:58
 **/
public class Utils {

    public static final DecimalFormat FORMAT = new DecimalFormat("000");

    @SneakyThrows
    public static void down(String url, String path, String referer) {
        if (!new File(path).exists()) {
            URLConnection connection = new URL(url).openConnection();
            connection.setRequestProperty("Referer", referer);
            Files.createDirectories(Paths.get(path).getParent());
            Files.write(Paths.get(path), connection.getInputStream().readAllBytes());
        }
    }

    @SneakyThrows
    public static void down(String url, String path) {
        if (!new File(path).exists()) {
            Files.createDirectories(Paths.get(path).getParent());
            Files.write(Paths.get(path), new URL(url).openConnection().getInputStream().readAllBytes());
        }
    }

    @SneakyThrows
    public static void checkHistory(String file1, String file2) {
        if (!new File(file1).exists()) {
            Files.createDirectories(Paths.get(file1).getParent());
            Files.writeString(Paths.get(file1), "1");
            Files.writeString(Paths.get(file2), "2000");
        }
    }
}

3 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.gzz</groupId>
    <artifactId>13-boot-jsoup</artifactId>
    <version>1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.16.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
ff6a6d6e93699d97514b5c2d2bcd04b.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容