javaJsoup爬取LOL英雄联盟全皮肤
配置maven
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
代码:
/**
*
*/
package TestJava;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
/**
* @author 杨光宇
* 2019年12月9日
*/
public class day9 {
public static void main(String[] args) throws Exception {
//获取元素的地址 返回doc对象
Document document = Jsoup.connect("http://lol.kuai8.com/hero/").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("D:/pic/lol/"+heroName);
//如果文件不存在那么创建文件
if(!file.exists()){
file.mkdirs();
}
//调用下载方法 传入链接和名字
try {
download(heroHref, heroName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("下载失败:"+heroHref);
}
}
System.out.println("一共"+i+"个英雄");
}
/**
* str 传入英雄的链接
* heroName 英雄名字用来下载地址
*/
public static void download(String str,String heroName) throws Exception{
//获取doc对象
Document doc = Jsoup.connect(str).get();
//查询
Elements select = doc.select("div.attribute-skin-bg img[src]");
for (Element element : select) {
//获取皮肤链接地址
String skinHerf = element.attr("data-original");
System.out.println(skinHerf);
//获取皮肤名字
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(skinHerf, heroName,sinkName));
}
}
/**
* str 传入皮肤的链接
* heroName 英雄名字用来下载地址
* heroName 皮肤名字
*/
public static String downImage(String str,String heroName, String sinkName) throws Exception{
//URL对象
URL url = new URL(str);
//链接
URLConnection con = url.openConnection();
InputStream in = null;
FileOutputStream fos = null;
try {
//获取流
in =con.getInputStream();
fos = new FileOutputStream(new File("D:/pic/lol/"+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 "下载完成";
}
}
效果展示:
结束语:
在下载的时候遇到了一些问题没有处理完毕,比如:java.io.FileNotFoundException: http://img.kuai8.com/attaches/picture/20151215/201512151940389774.jpg百度了一些说是拒绝访问,再一次下载是可以下载的,问题出在哪里也没有搞清楚。