package com.csu.test; import java.io.File; import java.io.IOException; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.apache.commons.io.FilenameUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; /** * * @author CSU-Max * 抓取指定网页的图片保存在指定文件夹 * */ public class Main { public static void main(String[] args) { //抓取的图片存放的文件夹 String filePath = "D:/CSU-Max-temp"; //要抓取图片的网页地址 String webUrl = "http://xiangce.baidu.com/picture/album/list/85062425ab359f0760819f33a14ec718884ce667"; File file = new File(filePath); if(file.exists()){ System.out.println("文件夹已经存在"); }else{ file.mkdir(); System.out.println("自动创建文件夹"); } try { Document doc = Jsoup.connect(webUrl).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31").get(); Elements imgElements = doc.getElementsByTag("img"); Set<String> imgSrcSet = new HashSet<String>(); for (Element img : imgElements) { String imgSrc = img.attr("abs:src"); imgSrcSet.add(imgSrc); } System.out.println("图片总数:"+imgSrcSet.size()); Iterator<String> i = imgSrcSet.iterator(); while(i.hasNext()){ String imgSrc = (String)i.next(); String imgName = FilenameUtils.getName(imgSrc); if (imgName.indexOf(".") != -1) { if (imgName.indexOf("?") > -1) { imgName = imgName.substring(0, imgName.indexOf("?")); } Util util = new Util(); String saveImagePath = filePath+"/"+imgName; System.out.println("图片抓取开始:"); util.download(imgSrc,saveImagePath); System.out.println("图片抓取结束:"+imgSrc+" 保存路径:"+saveImagePath); } } } catch (IOException e) { e.printStackTrace(); } } }

zhangguobin LV14
2019年9月10日
1662530129 LV12
2019年8月12日
yihang LV7
2019年2月3日
dagf113225 LV68
2019年1月9日
756398953 LV12
2018年11月15日
冬瓜Summit LV10
2018年6月11日
360763120 LV11
2018年4月27日
lindawhn LV6
2018年4月12日
xl921005 LV1
2017年6月16日
yly123 LV17
2017年3月29日