给爷爪巴,Jsoup爬图片

先放图

我图没了,说我sq,直接看代码吧,原来网址也不行…

上代码

public static void main(String[] args) {
        String url = "lmth.utiem/moc.0004niw.www//:ptth";
        getMoreUrls(url);
    }
    private static void getMoreUrls(String url) {//获取当前页面的  更多按钮的url
        List moreUrlList = new ArrayList();
        Document document = null;
        try {
            document = Jsoup.connect(url).timeout(6000).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (document != null) {

            Elements elements = document.getElementsByTag("a");
            for (Element e: elements) {
                if (e.attr("href").contains("tag")){
                    String moreUrl = e.attr("href");
                    int UrlSize = getMoreUrlSize(moreUrl);
                    for (int i = 1; i <= UrlSize; i++) {
                        String minUrl = moreUrl.substring(0,33)+i+".html";
                        moreUrlList.add(minUrl);
                    }

                }
            }
            for (int i = 0; i < moreUrlList.size(); i++) {
                down(moreUrlList.get(i).toString());
            }
        }

    }

    private static int getMoreUrlSize(String url) { //获取更多按钮的数量

        Document document = null;
        try {
            document = Jsoup.connect(url).timeout(6000).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        List lastList = new ArrayList();
        if (document != null) {
            Elements elements = document.getElementsByClass("num");

            for (Element e: elements) {
                if (e.attr("rel")!= null && !e.attr("rel").equals("")){
                    lastList.add(e.html());
                }
            }


        }


        return Integer.parseInt(lastList.get(lastList.size()-1).toString());

    }

    private static void down(String url) {//获取当前页面 所有图片的url

        Document document = null;
        try {
            document = Jsoup.connect(url).timeout(6000).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (document != null) {
            Elements elements = document.getElementsByTag("a");
            for (Element e: elements) {
                if (e.attr("href").contains("meinv") && e.attr("href").contains("http")
                        && !e.attr("href").contains("tag")){
                    String URL = e.attr("href");
                    String TITLE = e.children().first().attr("alt");
                    getNum(URL,TITLE);
                }
            }
        }
    }

    private static void getNum(String url,String title) {//获取某一系列的url
        Document document = null;
        try {
            document = Jsoup.connect(url).timeout(6000).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (document != null) {
            int num = Integer.parseInt(document.getElementsByTag("em").first().html());
            for (int i = 1; i <= num; i++) {
                String URL = url.split(".html")[0]+"_"+i+".html";
                getLarge(URL,title,i);
            }
        }

    }
    private static void getLarge(String url,String title,int num) {//通过url 获取当前页面的图片的大图src
        Document document = null;
        try {
            document = Jsoup.connect(url).timeout(6000).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (document != null) {
            Elements elements = document.getElementsByClass("pic-large");
            String URL =elements.get(0).attr("url");

            save(URL,num,title);
        }
    }
    private static void save(String url,int title,String floder){ //保存到电脑
        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection) new URL(url).openConnection();
            if (connection.getResponseCode() == 200) {
                InputStream put = connection.getInputStream();
                String path = "D:\\桌面\\img\\"+floder;
                File file0 = new File(path);
                file0.mkdir();
                File file = new File(path+"\\"+title+".jpg");
                if (!file.exists()) file.createNewFile();
                OutputStream out = new FileOutputStream(file);
                byte[] buff = new byte[1024];
                int stream;
                while ((stream = put.read(buff)) > 0) out.write(buff, 0, stream);
                put.close();
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(floder+title+"保存成功");
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值