• Java中如何使用Jsoup进行HTML页面的简单爬虫(爬取图片)


    准备工作:

                   1. 一个HTML页面,这里以 https://www.qycn.com/xzx/article/3969.html 为例

         2. 新建一个maven工程,引入jsoup依赖

    <!-- jsoup HTML parser library @ https://jsoup.org/ -->
      <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.13.1</version>
       </dependency>  

     页面图片如图所示:

     然后F12需要找到包裹图片的外部DIV的Class属性名称,该名称用来帮助获取图片位置

     准备工作已就绪,本次功能未下载当前HTML页面上的所有图片,完整代码如下:

    package com.jsoup.com.jsoup;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.select.Elements;
     
    /**
     * 
    * @ClassName: JsoupUtil 
    * @Description: 图片爬虫 -- 使用jsoup解析html页面,获取需要的路径,进行循环下载
     */
    public class JsoupUtil {
        
        //定义路径
        static String baseurl = "https://www.qycn.com";
        static String geturl = "https://www.qycn.com/xzx/article/3969.html";
        static String filepath = "C:\\Users\\Justin\\Desktop\\linshi\\images\\";
     
        public static void main(String[] args) {
            System.out.println("初始下载页面:"+geturl);
            String html = getHtml(geturl); // html页面内容
            List<String> srclists = getImgSrcListFromHtml(html); // 图片地址集合
            downloadImg(srclists, filepath); // 下载图片
            // 获取下一个页面进行下载
    /*        List<String> list = getNextPageUrl(html);
            System.out.println(list.size());
            for (int i = 0; i < list.size(); i++) {
                String url = list.get(i);
                System.out.println("下一个下载页面:" + url);
                String html2 = getHtml(url); // html页面内容
                List<String> srclists2 = getImgSrcListFromHtml(html2); // 图片地址集合
                downloadImg(srclists2, filepath); // 下载图片
            }*/
            System.out.println("下载完毕");
        }
        
        /**
         * 
        * @Title: getHtml 
        * @Description: 获取页面内容
        * @param @param url
        * @param @return  页面内容
        * @return String  返回类型 
        * @throws
         */
        public static String getHtml(String url){
            String html = "";
            try {
                html = Jsoup.connect(url).execute().body();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return html;
        }
        
        /**
         * 
        * @Title: getImgSrcListFromHtml 
        * @Description: 获取页面内容图片路径
        * @param @param html  页面内容
        * @param @return    图片路径数组
        * @return ArrayList<String>    返回类型 
        * @throws
         */
        public static List<String> getImgSrcListFromHtml(String html){
            List<String> list = new ArrayList<>();
            //解析成html页面
            Document document = Jsoup.parse(html);
            //获取目标
            Elements elements = document.select("div [class=qyzx_note]").select("img");
            int len = elements.size();
            for (int i = 0; i < len; i++) {
           // list.add(elements.get(i).attr("src")); list.add(baseurl
    + elements.get(i).attr("src")); } return list; } /** * * @Title: getNextPage * @Description: 从页面内容中获取下一个页面路径 * @param 页面内容 * @return List<String> 返回页面url数组 * @throws */ public static List<String> getNextPageUrl(String html){ List<String> list = new ArrayList<>(); //解析成html页面 Document document = Jsoup.parse(html); //获取目标 Elements elements = document.select("div [class=list]").select("a"); for (int i = 0;i<elements.size();i++) { String url = baseurl + elements.get(i).attr("href"); list.add(url); } return list; } /** * * @Title: downloadImg * @Description: 下载图片 -- 通过获取的流转成byte[]数组,再通过FileOutputStream写出 * @param @param list 图片路径数组 * @param @param filepath 保存文件夹位置 * @return void 返回类型 * @throws */ public static void downloadImg(List<String> list, String filepath){ URL newUrl = null; HttpURLConnection hconnection = null; InputStream inputStream = null; FileOutputStream fileOutputStream = null; byte[] bs = null; try { int len = list.size(); for (int i = 0; i < len; i++) { String finalFilePath = ""; newUrl = new URL(list.get(i)); hconnection = (HttpURLConnection) newUrl.openConnection(); //打开连接 hconnection.setRequestMethod("GET"); hconnection.setRequestProperty("Content-Type", "application/json"); hconnection.setRequestProperty("connection", "keep-alive"); hconnection.setRequestProperty("User-Agent", "Mozilla/4.76"); inputStream = hconnection.getInputStream(); //获取流 bs = getBytesFromInputStream(inputStream); //流转btye[] finalFilePath = filepath + list.get(i).substring(list.get(i).lastIndexOf("/")+1); //获取图片名称 System.out.println("生成图片路径:"+finalFilePath); fileOutputStream = new FileOutputStream(new File(finalFilePath)); fileOutputStream.write(bs); //写出 } } catch (Exception e) { e.printStackTrace(); } finally { try { inputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @Title: getBytesFromInputStream * @Description: InputStream流转换byte[] * @param @param inputStream * @param @return byte[] * @return byte[] 返回类型 * @throws */ public static byte[] getBytesFromInputStream(InputStream inputStream){ byte[] bs = null; try { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); // while((len = inputStream.read(buffer)) != -1){ arrayOutputStream.write(buffer, 0 ,len); } bs = arrayOutputStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } return bs; } }

    结果展示如图:

    参考来自:https://blog.csdn.net/qq_37902949/article/details/81257065

  • 相关阅读:
    僵尸进程
    理论整理
    SQLServer相关概念
    存储过程
    我的简书地址
    swift pragma mark
    苹果iOS开发中如何直接跳转到App Store页面
    使用cocoadPod updating local specs repositories 卡主
    iOS 代码格式化插件Clang-Format
    iOS错误:AFNetworking Error Domain=NSURLErrorDomain Code=-999
  • 原文地址:https://www.cnblogs.com/funian/p/16375471.html
Copyright © 2020-2023  润新知