指针原来是套娃的


私信TA

用户名:uq_92467646842

访问量:44211

签 名:

数学改变科学,科学改变世界

等  级
排  名 10
经  验 25292
参赛次数 49
文章发表 128
年  龄 0
在职情况 学生
学  校
专  业 物联网工程

  自我简介:

QQ:2830671713

//记录一下,防止重写

import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import net.minidev.json.JSONUtil;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.LogManager;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetLolPosters {
    public static void main(String[] args) throws IOException {

        LogManager.getLogManager().reset();//加上这条语句隐藏HtmlUnit执行后的报错信息

        String url = "https://lol.qq.com/data/info-heros.shtml";

        WebClient webClient = new WebClient();
        HtmlPage page = null;
        try {
            page = webClient.getPage(url);//尝试加载网页
            //异步JS执行需要耗时,这里线程要阻塞300ms,等待异步JS执行结束
            webClient.waitForBackgroundJavaScript(300);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            webClient.close();
        }

        System.out.println(page.asXml());
        //获取html文档
        Document document = Jsoup.parse(page.asXml());
        //获取英雄列表,不使用WebClient会导致列表加载不全
        Element elementUl = document.selectFirst("[class=imgtextlist]");
//        System.out.println(elementUl);
        //通过ul标签找到它下面所有的li子标签,根据标签名查找
        Elements elementLis = elementUl.select("li");
        System.out.println(elementLis);
        System.out.println("第一个页面已分析好");

        int size1 = 0;//记录英雄个数
        int size2 = 0;//记录图片个数
        for (Element elementLi : elementLis) {
            size1++;//一个循环为一个英雄
            //选中a标签
            Element elementA = elementLi.selectFirst("a");
            // 获取a标签中的href属性值储存着该英雄的页面地址
            String href = elementA.attr("href");//获取标签中的属性值
            String heroName = elementA.attr("title");//获取英雄名字,用来做文件夹名
            String[] ids = href.split("=");
            System.out.printf(ids[1]);

            String netPath = "https://lol.qq.com/data/" + href;
            //很不幸,英雄皮肤列表也是动态加载
            //所以只能按固定地址下载第一张照片
            String str = "https://game.gtimg.cn/images/lol/act/img/skin/big" + ids[1] + "000.jpg";
            System.out.println("正在下载" + str);
            URL url2 = new URL(str);
            InputStream inputStream = url2.openStream();
            FileOutputStream fileOutputStream = new FileOutputStream("D://英雄联盟//" + size1 + heroName + ".jpg");

            //需要创建一个byte数组
            byte[] b = new byte[1024];
            //读取到的数据临时存放在b字节数组内
            int count = inputStream.read(b);//count 指的是读取的有效字节个数
            while (count != -1) {
                fileOutputStream.write(b, 0, count);

                fileOutputStream.flush();

                count = inputStream.read(b);
            }
            fileOutputStream.close();
            inputStream.close();
            size2++;//记录总共下载的图片个数
        }
        System.out.println("结束,共有" + size1 + "个英雄," + size2 + "张照片");

    }
}


 

0.0分

154 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区