package Jsoups;
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 java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
public class spider01 {
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://pic.yesky.com/c/6_25156.shtml");
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
String utf8 = EntityUtils.toString(response.getEntity(), "utf8");
Document document = Jsoup.parse(utf8);
Elements elements = document.getElementsByClass("tabList");
int count = 0;
for (Element element : elements) {
for (Element element1 : element.getElementsByTag("li")) {
for (Element img : element1.getElementsByTag("img")) {
String src = img.attr("src");
URL url = new URL(src);
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream outputStream = new FileOutputStream("E:\\Picture\\" + (count++) + ".png");
int temp = 0;
while ((temp = inputStream.read()) != -1) {
outputStream.write(temp);
}
System.out.println(count + ".png下載完成");
outputStream.close();
inputStream.close();
}
}
}
}
}
}