import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import demo.DownloadPicFromURL;
public class JsoupDemo {
public static void main(String[] args) throws IOException {
// https://pvp.qq.com/web201605/herodetail/106.shtml
Document document=Jsoup.connect("https://pvp.qq.com/web201605/herolist.shtml")
.userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")
.get();
Elements select = document.select("[class=herolist clearfix]");
Elements select2 = select.select("li");
for (Element element : select2) {
int i = 1;
String attr = element.select("a").attr("href");
String text = element.select("a").text();
//https://pvp.qq.com/web201605/herodetail/106.shtml
//https://pvp.qq.com/web201605/herodetail/173.shtml
System.out.println("进去的lianje:" + "https://pvp.qq.com/web201605/" + attr);
// 进入英雄介绍
Document document2=Jsoup.connect("https://pvp.qq.com/web201605/"+attr)
.userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")
.get();
//https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/106/106-bigskin-1.jpg
// https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/184/184-bigskin-2.jpg
String attr2 = document2.select("[class = zk-con1 zk-con]").attr("style");
String yxdmj = attr2;
System.out.println(yxdmj);
// 初始背景
String substring = attr2.substring(16, attr2.length() - 11);
// 获取英雄代码
// adder.substring(69, adder.length() - 29);
String yxdm = substring.substring(53, 56);
DownloadPicFromURL sd = new DownloadPicFromURL();
String path = "d:/test/" + text + ".jpg";
sd.downloadPicture("https:" + substring, path);
// 获取皮肤背景
Elements con1 = document2.select("[class = con1-pic]");
Elements showbox = con1.select("[class = pic-show-box]");
Elements pf = showbox.select("[class= pic-pf]");
Elements pful = pf.select("ul");
for (Element pful2 : pful) {
String pfmc = pful2.select("[class = pic-pf-list pic-pf-list3]").attr("data-imgname");
String[] split = pfmc.split("\\|");
int a = split.length;
for (String pfmclb : split) {
System.out.println(pfmclb);
// game.gtimg.cn/images/yxzj/img201606/skin/hero-info/108/108-bigskin-1.jpg
// https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/106/106-bigskin-1.jpg
// https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/184/184-bigskin-2.jpg
if (i > a) {
break;
}
String patha = "d:/test/" + pfmclb + ".jpg";
sd.downloadPicture(
"https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/" + yxdm + "/" + yxdm
+ "-bigskin-" + i + ".jpg",
patha);
i++;
}
}
}
}
}
下载文件代码
package demo;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class DownloadPicFromURL {
//链接url下载图片
public void downloadPicture(String urlList, String path) {
URL url = null;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}