• Java jsoup获取网页中的图片


    获取图片

    package com.vfsd.net;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    /**
     * Servlet implementation class GetImgFromHtml
     */
    @WebServlet("/GetImgFromHtml")
    public class GetImgFromHtml extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public GetImgFromHtml() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            //response.getWriter().append("Served at: ").append(request.getContextPath());
            getDataHTML1();
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
        
        public static void getDataHTML1() throws IOException{
            String aHrefStr = "http://www.xxx.com/youji/123.html";
            System.out.println(aHrefStr);
            
            String dirNameIndex = "1";
            System.out.println(dirNameIndex);
            int dataSize1 = getImg(aHrefStr,dirNameIndex);
            //int dataSize2 = getImg1(aHrefStr,dirNameIndex);
            
        }
        
        public static int getImg(String stringUrl,String dirNameParam) throws IOException {
            String agent1 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";
            
            Document documentRoot = Jsoup.connect(stringUrl).userAgent(agent1).get();
            
            //System.out.println(documentRoot);
            
            //Elements imgElements = documentRoot.getElementsByClass("lazy");
            Elements imgElements = documentRoot.getElementsByTag("img");
            List<String> list = new ArrayList<String>();
            
            System.out.println("==="+imgElements.size());
            int i = 0;
            for (Element imgElement : imgElements) {
                //imgElement.getElementsByClass("");
                String imgSrcStr = imgElement.attr("data-original");//获取到src的值
                String imgUrl="";
                
                String imgName = getImgNameByTime(dirNameParam);
           System.out.println(imgName);
        } 
      }

    }
  • 相关阅读:
    求质数与因式分解
    利用工具解题
    我的学生信息管理系统总结
    JSP简单练习-页面重定向
    CSS学习(三)—相对定位与绝对定位
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    AVL平衡树的插入例程
    SAE搭建WordPress教程 免费建WordPress博客站
    MPMoviePlayerViewController和MPMoviePlayerController的使用
    Android日志输出工具类
  • 原文地址:https://www.cnblogs.com/herd/p/11991752.html
Copyright © 2020-2023  润新知