批量获取微信公众号的文章图片保存在本地

package com.qboshi.springboot7;

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.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

public class NewTest {
    public static void main(String[] args) throws Exception {

        int id = 0;
        while (true) {
            //String url = "https://mp.weixin.qq.com/s?__biz=MzI4NzA4MzM3Nw==&mid=2650679934&idx=1&sn=7fb7ee866c7b08b050015bb1f439235e&chksm=f3d99460c4ae1d76d5f9dba0044f2d8274cfe7b087f35831495aa24f8eba4ae86b62790cba50&scene=27";
            System.out.println("请输入微信公众号文章地址:");
            Scanner scanner = new Scanner(System.in);
            String url = scanner.next();
            Document parse = Jsoup.parse(new URL(url), 1000);
            Element elementById = parse.getElementById("js_content");
            Elements srcs = elementById.getElementsByTag("img");
            for (Element src : srcs) {
                String srcimanges = src.attr("data-src");
                //System.out.println(srcimanges);
                //获取输入流
                URL target = new URL(srcimanges);
                URLConnection urlConnection = target.openConnection();
                InputStream inputStream = urlConnection.getInputStream();
                id++;
                OutputStream outputStream = new FileOutputStream("F:\\images\\" + id + ".jpg");
                int temp = 0;
                while ((temp = inputStream.read()) != -1) {
                    outputStream.write(temp);
                }

                System.out.println(id + ".jpg下载完毕");
                outputStream.close();
                inputStream.close();
            }
            //System.out.println(elementById);
        }
    }
}

 

posted @ 2022-06-19 18:53  kliziM  阅读(397)  评论(1)    收藏  举报