1.下载依赖
<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.feiyangedu.sample</groupId>
<artifactId>hellodep</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.1.1</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>
</dependencies>
</project>
2.解析图片地址
https://www.ivsky.com/tupian/shenqiu_luoye_v58110/
确定图片元素路径,以用于解析。
注意需要将//img.ivsky.com/img/tupian/t/201909/09/shenqiu_luoye.jpg替换为pre。因为t是缩略图,而pre是原图。
3.示例
package com.testerhome;
import io.restassured.response.Response;
import org.apache.http.HttpConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.testng.annotations.Test;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
public class GetScenery {
@Test
public void getAutumn(){
String des = "autumn";
String url = "https://www.ivsky.com/tupian/shenqiu_luoye_v58110/";
File desDir = new File(des);
if(!desDir.exists() || !desDir.isDirectory()){
desDir.mkdir();
}
ArrayList<String> imgUrls = getPic(url);
for(int i=0;i<imgUrls.size();i++){
downPic(imgUrls.get(i),des);
System.out.println(imgUrls.get(i));
}
}
public ArrayList<String> getPic(String url){
ArrayList<String> result = new ArrayList<>();
String select = "body > div:nth-child(4) > div.left > ul";
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response ;
HttpGet request = new HttpGet(url);
request.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");
try{
response = httpClient.execute(request);
HttpEntity httpEntity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String html = EntityUtils.toString(httpEntity, "gbk");
Document document = Jsoup.parse(html);
Elements pics = document.select("div.left>ul.pli>li");
for(Element pic:pics){
Elements img = pic.select("div.il_img>a>img");
String imgUrl = "http:"+img.attr("src");
//t是缩略图,pre是大图
result.add(imgUrl.replaceAll(".?t/","/pre/"));
}
}
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return result;
}
public void downPic(String url,String dir){
String fileName = dir + url.substring(url.lastIndexOf("/"));
try {
FileOutputStream fop = new FileOutputStream(fileName);
URL imgUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imgUrl.openConnection();
InputStream fip = conn.getInputStream();
File file = new File(fileName);
byte[] buffer = new byte[2048];
for(;;){
int hasRead = fip.read(buffer);
if(hasRead <0){
break;
}
fop.write(buffer,0,hasRead);
}
}catch (Exception e){
e.printStackTrace();
}
}
}