package com.simon.getimage; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.*; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; /** * @author Simon */ public class GetLolImg { /** * 代理开关: * TRUE:开启HTTP代理 * FALSE;关闭HTTP代理 */ private static final boolean SWITCH_PROXY = Boolean.TRUE; /** * 代理服务器地址 */ private static final String PROXY_IP = "XXXX.XXXX.XXXX"; /** * 代理服务器端口 */ private static final Integer PROXY_PORT = 8080; /** * 图片本地存储地址 */ private static final String IMG_LOCAL = "D:/lol/"; /** * 英雄联盟快吧专区地址 */ private static final String IMG_SOURCE = "http://lol.kuai8.com/hero/"; public static void main(String[] args) throws Exception { // 获取元素的地址 返回doc对象 Document document; if (SWITCH_PROXY) { document = Jsoup.connect(IMG_SOURCE).proxy(PROXY_IP, PROXY_PORT).get(); } else { document = Jsoup.connect(IMG_SOURCE).get(); } // 查找 链接中有title的链接 Elements elements = document.select("a[title]"); // 计算有多少个英雄 int i = 0; // 循环标签 for (Element element : elements) { i++; // 获取标签href的内容 即是英雄的详细链接 String heroHref = element.attr("href"); // 获取标签title的内容 即是英雄的名字 String heroName = element.attr("title"); // 创建文件 File file = new File(IMG_LOCAL + heroName); // 如果文件不存在那么创建文件 if (!file.exists()) { file.mkdirs(); } // 调用下载方法 传入链接和名字 try { download(heroHref, heroName); } catch (Exception e) { e.printStackTrace(); System.out.println("下载失败:" + heroHref); } } System.out.println("一共" + i + "个英雄"); } public static void download(String str, String heroName) throws Exception { Document doc; if (SWITCH_PROXY) { doc = Jsoup.connect(str).proxy(PROXY_IP, PROXY_PORT).get(); } else { doc = Jsoup.connect(str).get(); } Elements select = doc.select("div.attribute-skin-bg img[src]"); for (Element element : select) { // 获取皮肤链接地址 String skinHref = element.attr("data-original"); System.out.println("皮肤链接:" + skinHref); // 获取皮肤名字 String sinkName = element.attr("alt"); System.out.println("皮肤名字:" + sinkName); // 这里有kda系列皮肤需要替换字符 if (sinkName.contains("/")) { System.out.println("替换:" + sinkName); sinkName = sinkName.replaceAll("\\/", ""); System.out.println("替换成功:" + sinkName); } System.out.println(downImage(skinHref, heroName, sinkName)); } } /** * @param str 传入皮肤的链接 * @param heroName 英雄名字 * @param sinkName 皮肤名字 * @return * @throws Exception */ public static String downImage(String str, String heroName, String sinkName) throws Exception { URL url = new URL(str); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_IP, PROXY_PORT)); URLConnection con; if (SWITCH_PROXY) { con = url.openConnection(proxy); } else { con = url.openConnection(); } InputStream in = null; FileOutputStream fos = null; try { in = con.getInputStream(); System.out.println("英雄名字:" + heroName); fos = new FileOutputStream(new File(IMG_LOCAL + heroName + "/" + sinkName + ".jpg")); byte[] b = new byte[1024]; int len = -1; while ((len = in.read(b)) != -1) { fos.write(b, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } return "下载完成"; } }