1.下载依赖
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.feiyangedu.sample
hellodep
0.0.1-SNAPSHOT
jar
UTF-8
1.8
1.8
1.8
javax.mail
1.4
com.fasterxml.jackson.core
jackson-databind
2.9.0
com.fasterxml.jackson.datatype
jackson-datatype-jsr310
2.9.0
mysql
mysql-connector-java
8.0.17
com.zaxxer
HikariCP
3.3.1
org.apache.poi
poi
3.17
org.apache.poi
poi-ooxml
3.17
org.testng
testng
6.14.3
compile
io.rest-assured
rest-assured
4.1.1
compile
org.hamcrest
hamcrest-all
1.3
test
org.hamcrest
hamcrest-core
1.3
test
org.apache.httpcomponents
httpclient
4.5.8
org.jsoup
jsoup
1.10.3
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 imgUrls = getPic(url);
for(int i=0;i
downPic(imgUrls.get(i),des);
System.out.println(imgUrls.get(i));
}
}
public ArrayList getPic(String url){
ArrayList 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();
}
}
}
标签:Java,String,http,保存,风景图片,org,apache,import,com
来源: https://www.cnblogs.com/csj2018/p/12389858.html