1 温馨提示:jdk1.8+ ,准备好大硬盘,全站美女套图全归你
2 源码下载
码云:https://gitee.com/gao_zhenzhong/spring-boots/tree/master/ok/98-app/pictures
3 运行程序后 地址栏输入 http://localhost:9090 如下图所示
image.png
image.png
4 推荐使用ACDSee查看图片
5 准备纸巾
6 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>spring-boot-girl</artifactId>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>aliyunmaven</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>
</project>
7 Application18.java
package com.gzz;
import java.text.DecimalFormat;
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.extern.slf4j.Slf4j;
@Slf4j
public class Application18 {
private static String rootpath = "D:/aaa/";
private static int num;
private static DecimalFormat df = new DecimalFormat("00");
private static String baseUrl = "https://www.wxytw.com/jgmntp/yuhuajie/page/";
public static void main(String[] args) throws Exception {
for (int i = 1; i < 34; i++) {
Document document = Jsoup.connect(baseUrl + i).get();
//log.info(document);
Elements selects = document.select("a.thumbnail");
for (Element element : selects) {
String href = element.attr("href");
String setPath = element.select("img").first().attr("alt");
log.info(setPath);
Document pages = Jsoup.connect(href).get();
//log.info(document);
Elements pics = pages.select("a.post-page-numbers");
String pageNum = pics.last().select("span").first().text();
//log.info(pageNum);
num = 0;
for (int j = 1; j <= Integer.parseInt(pageNum); j++) {
pages = Jsoup.connect(href + "/" + j).get();
Elements images = pages.select(".article-content img");
for (Element image : images) {
//log.info(image.attr("src"));
String path = rootpath + setPath + "/" + df.format(num) + ".jpg";
Utils.down(image.attr("src"), path, href);
num++;
}
}
}
}
}
}
8 下载工具类
package com.gzz.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Utils {
public static void down(String url, String path, String referer) {
try {
File file = new File(path);
if (!file.exists()) {
URLConnection connection = new URL(url).openConnection();
connection.setRequestProperty("Referer", referer);
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[1000];
int rc = 0;
InputStream inputStream = connection.getInputStream();
while ((rc = inputStream.read(buff, 0, 1000)) > 0) {
swapStream.write(buff, 0, rc);
}
Files.write(Paths.get(path), swapStream.toByteArray());
inputStream.close();
}
} catch (IOException e) {
log.error("抛锚了...", path, url, e);
}
}
}
9 页面文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>down-picture</title>
</head>
<script type="text/javascript" src="vue.min.js"></script>
<script type="text/javascript" src="axios.min.js"></script>
<body>
<div id="app">
<template v-for="item in dicts" :key="item.url">
<span style="width:220px;height:20; display:inline-block;border:1px solid black;padding:6px" >
<button :disabled="item.pageCount > '0'" @click="queryCount(item)" style="width:130px;" >{{item.label}}[共{{item.pageCount}}页]</button>
<button :disabled="!item.pageCount" @click="downPictures()">下载</button>
</span>
</template>
</div>
<script>
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
new Vue({
el: '#app',
data: function () {
return { form: { setName: '', pageCount: '', setUrl: '', }, dicts: [], messages: [] }
},
created: function () { this.queryDicts(); },
methods: {
queryCount(item) {
console.log("item", item)
this.form.setName = item.label;
this.form.setUrl = item.url;
axios.post("/queryCount?url=" + item.url).then(res => {
if (res.data.code == 200) {
this.form.pageCount = res.data.data;
item.pageCount = res.data.data;
}
else this.$message.error(res.data.msg);
});
},
queryDicts() {
axios.post("/queryDicts").then(res => {
if (res.data.code == 200) { this.dicts = res.data.data; }
else this.$message.error(res.data.msg);
});
},
downPictures() {
axios.post("/downPictures", JSON.stringify(this.form)).then(res => {
if (res.data.code == 200) { this.$message.success('已开始下载有耐心等待') }
else this.$message.error(res.data.msg);
})
},
},
});
</script>
</body>
</html>