根据网页的URL爬取网页上的图片,并打包生成压缩文件(HtmlUtil+Jsoup+ZipOutPutStream)
1.获取网页JS动态加载后的内容用到了HtmlUtil
2.根据解析后的XML获取指定标签内容用到了Jsoup
3.最后生成压缩文件用到了ZipOutputStream
package com.wl.test3; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; /** * 根据网页URL获取其网页上的图片,并生成打包文件 1.根据网页URL解析HTML获取指定标签内容图片SRC--HtmlUtil+Jsoup * HtmlUtil根据URl模拟浏览器获取网页js动态加载后的内容,Jsoup根据HtmlUtil解析后的XML文件获取指定的标签内容 * 2.根据图片地址下载并生成压缩文件--ZipOutPutStream 根据图片的地址下载图片到指定文件夹,使用ZipOutPutStream压缩流将其压缩 * * @author Administrator * */ public class PicFileCompression { /** * HtmlUtil+Jsoup根据网页URL获取动态加载后的页面并得到指定标签的内容 * @param url 网页地址 * @return * @throws Exception */ public static List<String> getPicSrc(String url) throws Exception { List<String> srcList = new ArrayList<String>(); /** 创建模拟指定浏览器的客户端对象 */ final WebClient webClient = new WebClient(BrowserVersion.CHROME); /** JS执行出错不抛出异常 */ webClient.getOptions().setThrowExceptionOnScriptError(false); /** HTTP状态不是200时不抛出异常 */ webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); /** 不启用CSS */ webClient.getOptions().setCssEnabled(false); /** 启用JS(非常重要) */ webClient.getOptions().setJavaScriptEnabled(true); /** 支持AJAX(非常重要) */ webClient.setAjaxController(new NicelyResynchronizingAjaxController()); /** JS执行需要一定时间,设置等待时间(非常重要) */ webClient.waitForBackgroundJavaScript(10000); // webClient.getOptions().setActiveXNative(false); // webClient.getOptions().setTimeout(10000); /** 加载网页 */ HtmlPage page = webClient.getPage(url); // Thread.sleep(3000); /** 将加载的网页转换成XML形式 */ String pageXml = page.asXml(); /** Jsoup获取HTML文档 */ Document document = Jsoup.parse(pageXml); /** 直接获取IMG标签 */ List<Element> infoListFile = document.getElementsByTag("img"); /** 获取IMG的SRC属性 */ for (Element img : infoListFile) { String src = img.attr("src"); if (!src.isEmpty()) { if ((!src.contains("http:")) && (!src.contains("https:"))) { src = "http:" + src; } System.out.println("图片地址:" + src); srcList.add(src); } } webClient.close(); return srcList; } /** * 根据图片的URL获取图片,并打包生成一个压缩文件 * @param src 图片地址 * @throws Exception */ public static void picZip(List<String> src) throws Exception { /** 图片下载后的保存地址 */ File file = new File("F:/Pictures.zip"); /** 若目录或文件不存在,则创建一个 */ if (!file.exists()) { file.getParentFile().mkdirs(); file.createNewFile(); } /** 压缩流,将写入输出流的内容压缩输出,生成压缩包 */ ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file)); for (String s : src) { /** 每一张图片压缩后的文件名称 */ String picName = s.substring(s.lastIndexOf("/") + 1, s.length()); URL url = new URL(s); /** 获取URl连接 */ HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); /** 获取输入流 */ InputStream input = connection.getInputStream(); /** 设置每一个压缩内容的名称 */ out.putNextEntry(new ZipEntry(picName)); byte[] buffer = new byte[10]; int length; /** 将输入流中的内容读取出来,并写入输出流 */ while ((length = input.read(buffer)) > 0) { out.write(buffer, 0, length); } input.close(); } out.close(); System.out.println("压缩包已生成,地址:" + file.getAbsolutePath()); } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub List<String> picSrc = getPicSrc("https://ent.sina.com.cn/film/"); if (picSrc.size() > 0) { picZip(picSrc); } else { System.out.println("未获取到网页上任何图片!"); } } }