java爬取图片

写在前面的叫前言

  • 本帖子只是交流学习,如发现不妥,请联系删除

一、说明

  • 闲来无事,学习下爬东西,闲话少说,需要jar包(请自行下载,此处推荐jar包下载地址 mvnrepository.com)

  • 需要注意的事项,首先是观察下一页的地址栏规律,比如第一页;第二页;第三页
  1. https://www.ivsky.com/bizhi/nvxing/
  2. 女性明星壁纸 - 高清美女壁纸_第2页 (天堂图片网)
  3. 女性明星壁纸 - 高清美女壁纸_第3页 (天堂图片网)

二、工具类

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JsoupUtil {
    /**
     * Jspup工具类 url:采集的URL domian: 采集的域名
     */
    public static Document getDocument(String url, String domain) {
        int error_count = 0;
        Document doc = null;
        while (true) {
            if (error_count > 10) {
                break;
            }
            try {
                doc = Jsoup.connect(url).timeout(6000)
                        .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                        .header("Accept-Encoding", "gzip,deflate,sdch").header("Connection", "keep-alive")
                        .header("referer", domain).header("cookie", "data").followRedirects(true)
                        .userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").get();
            } catch (Exception e) {
                error_count++;
            }
            if (doc != null) {
                break;
            }
        }
        return doc;
    }

    public static Document parseHtml(String html) {
        return Jsoup.parse(html);
    }

}

三、爬取方法

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class TestSImage {

    public static final String PRE_URL = "https://www.ivsky.com/bizhi/nvxing/";// 目标网址

    public static final String SOURCE_HTML = "https://www.ivsky.com/";// 目标首页地址

    public static final String GENERATE_PATH = "E:\\2\\";// 本地保存的路径

    /**
     *
     * @param index
     *            组装下一页的计数变量,如index_1 index_2
     * @throws Exception
     *             使用递归的方式爬取图片
     */
    public static void getGirlImage(String detailHtml, String sourceHtml, String generatePath, int index)
            throws Exception {
        String url = detailHtml;
        Document doc = JsoupUtil.getDocument(url, sourceHtml);
        Element element = doc.getElementsByClass("ali").first();
        // 3.提取图片
        Document imgDoc = JsoupUtil.parseHtml(element.toString());
        Elements elements = imgDoc.select("img[src]");
        String picFile = generatePath;

        for (int i = 0; i < elements.size(); i++) {
            Element ele = elements.get(i);
            String src = ele.attr("src");// 获取到src的值
            src = "https:" + src;
            String name = src.substring(src.lastIndexOf("/") + 1, src.length());
            FileUtils.copyURLToFile(new URL(src), new File(picFile + "\\" + Math.random() * 10000 + name));
            if (i == elements.size() - 1) {// 如果为本页最后一张则组装目标页面地址
                index++;
                detailHtml = PRE_URL + "index_" + index + ".html";// 拼装下一页访问地址
                System.out.println(detailHtml);
                getGirlImage(detailHtml, sourceHtml, generatePath, index);// 继续调用获取资源
            }
        }
    }

    public static void main(String[] args) throws MalformedURLException, IOException, Exception {
        getGirlImage(PRE_URL, SOURCE_HTML, GENERATE_PATH, 0);
    }

四、效果图

  • 至此结束

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值