package cn.sp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* a example program to list links from URL
* @author pc
*
*/
public class ListLinks2 {
public static void main(String[] args) throws Exception {
String url = "https://www.xxinbao.com";
print("Fetching %s...",url);
Document doc = Jsoup.connect(url).get();
//获取后缀名为 jpg和png的图片元素集合
//Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g)]");
Elements images = doc.select("img");
print("发现目标 %s个",images.size());
for (Element ele : images) {
String imageUrl = ele.attr("src");
if(imageUrl.contains("gif")){
continue;
}
if(!imageUrl.contains("https")){
imageUrl = url+imageUrl;
}
System.out.println("======"+imageUrl);
URL url1 = new URL(imageUrl);
URLConnection connection = url1.openConnection();
InputStream in = connection.getInputStream();
String uuid = String.valueOf(System.currentTimeMillis()+new Random().nextInt(9));
String path = "E:\\logs";
String imageName = uuid+".jpg";
OutputStream out = new FileOutputStream(new File(path,imageName));
byte[] buff = new byte[1024];
int i = -1;
while( (i =in.read(buff)) != -1){
out.write(buff,0,i);
}
in.close();
out.close();
}
System.out.println("爬取图片完毕");
}
private static void print(String msg, Object... args) {
System.out.println(String.format(msg, args));
}
}
结果如下: