当前位置: 首页 > news >正文

JavaSE小实践1:Java爬取斗图网站的所有表情包

跟朋友聊天总会用到大量表情包,有些人甚至专门收集各种各样的表情包,看看谁能打败谁。今天我就用java爬取了一个斗图网站上的所有表情包,用来充实自己的表情包库。代码逻辑有可能并不完美,哈哈,也花了我几个小时才完成呢。
下载完所有图片,总共有225M.

思路:主要通过解析页面的源代码来获取图片的URL地址,然后通过图片地址下载图片到本地,所以要学会使用浏览器进行分析。

  1. 所用jar包:jsoup-1.8.1.jar
  2. 网站首页:https://doutushe.com/portal/index/index/p/1
  3. 浏览器:Chrome

1,获取网页源代码

    /**
     * 获取网页源代码
     * @author Augustu
     * @param url 网页地址
     * @param encoding 网页编码
     * @return    网页源代码
     */
    public static String getUrlResource(String url,String encoding) {
        //网页源代码,用String这个容器记录
        String htmlResource = "";
        //记录读取网页的每一行数据
        String temp = null;
        try {
            //1,找到网站地址
            URL theUrl = new URL(url);
            //2,建立起与网站的连接
            URLConnection urlConnection = theUrl.openConnection();
            //3,创建输入流,此处读取的是网页的源代码
            InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream(),encoding);
            //4,对输入流进行缓冲,加快读取速度
            BufferedReader reader = new BufferedReader(isr);
            //5,一行一行读取源代码,存到htmlResource中
            while((temp = reader.readLine()) != null) {
                htmlResource += temp;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return htmlResource;
    }

2,获取页面所有组图片的UrL地址

    /**
     * 获取页面所有组图片的UrL地址
     * @author Augustu
     * @param context 每个页面的urL
     * @return 获取页面所有组图片的UrL地址
     */
    public static String findPictureUrl(String context) {
        String temp = "";//暂时存储得到的每个url
        String pictureUrl = "";//得到所有URL
        //1,Jsoup将读取的网页源代码解析为Html文档,便可以使用Jsoup的方法操作html元素了,就像javascript一样
        Document document = Jsoup.parse(context);
        //2,观察网页源代码,发现每组图片都连接到了另一个URL地址,这个a标签的class为“link-2”
        Elements groupUrl = document.getElementsByClass("link-2");
        //3,遍历每个a标签,得到href
        for(Element ele: groupUrl) {
            //此处我发现每次Url都输出两次,也没找到原因,就用此方法先解决他
            if(ele.attr("href") == temp) {
                continue;
            }
            temp = ele.attr("href");
            //4,将所有URL存入String中,并使用空格分开,便于后面分割
            //本来我使用“|”分隔开来,分割的结果竟然是每个字符都分开了
            pictureUrl += "https://doutushe.com"+ele.attr("href")+" ";
        }
        return pictureUrl;
    }

3,下载单张图片

    /**
     * 下载单张图片
     * @param picturl 图片地址
     * @param filePath    下载路径
     * @param fileName    下载名
     */
    public static void downPicture(String picturl,String filePath,String fileName) {
        FileOutputStream fos = null;//输出文件流
        BufferedOutputStream bos = null;//缓冲输出
        File file = null;//创建文件对象
        File dir = new File(filePath);//创建文件保存目录
        Connection.Response response;
        try {
            //1,Jsoup连接地址,得到响应流,ignoreContentType表示忽略网页类型,如果不加会报错(默认只支持文本),因为我们页面是图片
            response = Jsoup.connect(picturl).ignoreContentType(true).execute();
            //2,将页面内容按字节输出
            byte[] img = response.bodyAsBytes();
            //3,写入本地文件中
            //判断文件目录是否存在,
            if(!dir.exists() ){
                dir.mkdir();//创建文件夹
            }
            file = new File(filePath+"\\"+fileName);//创建文件
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(img);//写入本地
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //4,释放资源
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }

4,下载所有图片

    /**
     * 下载所有图片
     * @author Augustu
     * @param pictureUrl 每组图片url
     */
    public static void downallPicture(String pictureUrl,String downLoadPath) {
        String picturl = "";
        String pictureName ="";//
        String[] pictureUrlArry = pictureUrl.split(" ");//图片组的url
        for(int i=0;i<pictureUrlArry.length;i++) {
            //遍历得到每组图片的url
            String pictureHtml = getUrlResource(pictureUrlArry[i],"utf-8");
            Document document = Jsoup.parse(pictureHtml);
            //得到该组图片的分类名称
            String dir =  document.getElementsByTag("blockquote").first().child(0).text();
            //该标签包含所有图片url
            Elements elements = document.getElementsByClass("lazy");
            for(Element ele: elements) {
                //得到每张图片url
                picturl = ele.attr("data-original");
                //观察源代码,发现获取的图片地址多了/themes/doutushe/Public/assets/images/doutushe-erweima.jpg,将其删除
                if(picturl.equals("/themes/doutushe/Public/assets/images/doutushe-erweima.jpg")) {
                    continue;
                }
                //得到每张图片的名字,别忘了加后缀
                pictureName = ele.attr("title")+".gif";
                //下载该图片
                downPicture(picturl,downLoadPath+"\\"+dir,pictureName);
            }
        }
    }

5,主函数运行代码

    public static void main(String[] args) {
        String context = "";
        //观察源代码,发现共有28个页面
        for(int i=1;i<=28;i++) {
            //获取每个页面
            context = getUrlResource("https://doutushe.com/portal/index/index/p/"+i+"","utf-8");
            //获取该页面所有组图片的url
            String pictureUrl = findPictureUrl(context);
            downallPicture(pictureUrl,"E:\\image\\表情包");
        }
        
    }
    

相关文章:

  • LeetCode——Implement Trie (Prefix Tree)
  • 从普通程序员到身价过百亿:追求长期价值的耐心,决定了你能走多远
  • Android图形显示系统——概述
  • WPF中查看PDF文件
  • Jenkins——持续集成服务器
  • JVM里面hashtable和hashmap实现原理
  • iOS 10 的推送 User Notifications Framework
  • .NET连接MongoDB数据库实例教程
  • rar自动压缩备份
  • Java_BigDecimal类型比较大小
  • 小程序使用smart模板的方法
  • LoadRunner上传文件脚本
  • Android自定义view双缓存技术
  • Linux命令行下运行java.class文件
  • nmap入门之其他
  • 2018一半小结一波
  • CentOS 7 修改主机名
  • Django 博客开发教程 8 - 博客文章详情页
  • gops —— Go 程序诊断分析工具
  • linux安装openssl、swoole等扩展的具体步骤
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • Python语法速览与机器学习开发环境搭建
  • tweak 支持第三方库
  • ViewService——一种保证客户端与服务端同步的方法
  • XForms - 更强大的Form
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 小程序开发之路(一)
  • 延迟脚本的方式
  • 以太坊客户端Geth命令参数详解
  • 翻译 | The Principles of OOD 面向对象设计原则
  • 关于Android全面屏虚拟导航栏的适配总结
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #WEB前端(HTML属性)
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (12)目标检测_SSD基于pytorch搭建代码
  • (2.2w字)前端单元测试之Jest详解篇
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (Note)C++中的继承方式
  • (编译到47%失败)to be deleted
  • (多级缓存)缓存同步
  • (论文阅读22/100)Learning a Deep Compact Image Representation for Visual Tracking
  • (企业 / 公司项目)前端使用pingyin-pro将汉字转成拼音
  • (转)Google的Objective-C编码规范
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .NET 药厂业务系统 CPU爆高分析
  • .NET 中使用 Mutex 进行跨越进程边界的同步
  • .net开发引用程序集提示没有强名称的解决办法
  • ::before和::after 常见的用法
  • ?php echo ?,?php echo Hello world!;?
  • @LoadBalanced 和 @RefreshScope 同时使用,负载均衡失效分析
  • @我的前任是个极品 微博分析
  • [AIGC] 如何建立和优化你的工作流?