使用jsoup批量抓取腾讯文档图片链接

使用腾讯文档excel表格上传图片,图片是以链接的形式,如何批量保存这些图片呢?

 1:将文档导出成excel到本地,使用wps打开,再另存为网页文件html


 

 

      2: 打开其中的一个html文件,可以看到,图片是以a 链接的形式存在

3 :使用jsoup批量抓取图片链接

      (1) 引入jsoup依赖

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.8.2</version>
        </dependency>

        (2) 完整的代码

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class JsoupTest {

    public static void main(String[] args) {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        InputStream cis = null;
        BufferedInputStream bis = null;
        try {
            Document document = Jsoup.parse(new File("E:\\mytest\\sheet002.htm"), "utf-8");
            Elements links = document.select("a[href]");
            for (Element link : links) {
                System.out.println("link : " + link.attr("href"));
                String href = link.attr("href");
                String name = link.text();
                URL url = new URL(href);
                URLConnection connection = url.openConnection();
                connection.connect();
                File file = new File("E:/mytest/" + name);
                //判断父目录是否存在
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdir();
                }
                if (!file.exists()) {
                    //先创建文件
                    file.createNewFile();
                    //写入文件
                    fos = new FileOutputStream(file);
                    bos = new BufferedOutputStream(fos);
                    cis = connection.getInputStream();
                    bis = new BufferedInputStream(cis);
                    byte[] bytes = new byte[8192];
                    int length = -1;
                    while ((length = bis.read(bytes)) != -1) {
                        bos.write(bytes, 0, length);
                    }
                }
            }
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (cis != null) {
                    cis.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 (3) 查看执行结果,图片抓取成功

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值