美文网首页
java不能写爬虫吗?海量美女图片任你选!

java不能写爬虫吗?海量美女图片任你选!

作者: 不知不怪 | 来源:发表于2023-07-07 15:10 被阅读0次

1 主程序

package com.gzz;

import static com.gzz.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 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://mmm.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();// 当前下载页
            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]", ""));// 当前图集编号
                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("div#masonry").select("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) + ".jpg";
                        Utils.down("https:" + images.get(j).attr("data-original"), 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

相关文章

网友评论

      本文标题:java不能写爬虫吗?海量美女图片任你选!

      本文链接:https://www.haomeiwen.com/subject/irfdudtx.html