批量下载微信网页图片

各位LSP,经常看微信公众号吧,有时候看下喜欢的图片,只能一张张右键下载吧,很是麻烦。
今天给我大家分享一下批量下载微信网页上的图片。
不过笔者是学Java的,代码是用Java写的,其他语言的话请各位大佬按照思路自己转换哈。

1.我们先找一个微信网页看看


https://mp.weixin.qq.com/s/E7XSfNIDom96KGPKkWxQFQ?token=928296293&lang=zh_CN
我们打开网页,然后按F12打开开发者模式,选择图片的位置,看下图片地址

可以看到图片是一个网址,那么我们把图片网址复制出来贴到浏览器上
https://mmbiz.qpic.cn/sz_mmbiz_jpg/4ZG39b77k2osG8ldxD8QjHlwvBLYfUekETfydx8h6SEgiccibWpVV9EJB3tqO7xKDLl5rugwY8wxFs0Itgk4oCTw/640?wx_fmt=jpeg&from=appmsg&tp=jpg&wxfrom=5&wx_lazy=1&wx_co=1
然后直接保存,看到是文件名称 640.jpg,那么这个图片就是 jpg 格式的图片,我们看下图片链接上刚好有jpg, 就是  wx_fmt=jpeg

2.我们之间 ctrl+s 保存整个网页,我们会得到一个html文件


![image.png](https://pic4.zhimg.com/v2-2794c8d6471687e3275d1c608fd3620f_r.jpg)
用editplus 打开这个html,然后直接找我们看的图片地址

直接找到了 img 标签,里面有 data-src 属性 图片地址 和 data-type 图片类型,如果我们之间解析这个html文件,提取里面的图片地址然后下载,保存为data-type 图片类型不就可以了吗?

3.写代码


``` type

package image;

import cn.hutool.core.io.FileUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;


/**
 * @Author: xiaocao
 * @CreateTime: 2025-01-21
 * @Description: 提取网页图片
 * @Version: 1.0
 */

public class ImageHtmlOkHttpWeixin2 {

    public static void main(String[] args) throws Exception {
        // 读取html 文件
        String htmlUrl = "E:\\my\\swmt\\微信批量下载图片\\日本性感女星白石麻衣 人间尤物 人美身材好 美女图写真集.html";
        //图片保存位置
        String baseUrl = "E:\\my\\swmt\\微信批量下载图片\\download";
        getFile(htmlUrl,baseUrl);


    }

    private static void getFile(String htmlUrl,String baseurl) throws Exception {
        // 读取html 文件
        String htmlContent = FileUtil.readString(htmlUrl, "UTF-8");
        Document doc = Jsoup.parse(htmlContent);
        // 查找所有的 <img> 标签
        Elements imgElements = doc.select("img");
        File dir = new File(baseurl);
        if (!dir.exists()) {
            //文件不存在就创建
            dir.mkdirs();
        }

        // 遍历每个 <img> 标签并提取 src 属性
        int count = 1;
        for (Element img : imgElements) {
            //获取图片地址
            String src = img.absUrl("data-src");
            if(!src.contains("http")){
                continue;
            }
            //获取图片类型
            String type = img.attr("data-type");
            if(type.isEmpty()){
                continue;
            }
            System.out.println("Image URL: " + src +"  图片类型:" + type);
            //下载图片重命名
            downloadImage(src, dir + "\\" + count + "."+type);
            count++;
        }
    }

    private static void downloadImage(String imageUrl, String fileName) throws Exception {
        try (InputStream in = new URL(imageUrl).openStream();
             OutputStream out = Files.newOutputStream(Paths.get(fileName))) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            System.out.println("图片保存位置: " + fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


```
上面代码需要修改 网页的位置和图片保存位置,然后运行 main 方法。
祝各位成功,有问题可以留言。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值