• Java爬虫框架 | 爬小说


    Jsoup,Java爬虫解决方案,中文文档:jsoup

      不得不说Java的生态真的好,原来我以为爬虫是只能用Pyhton来写的,结果发现Java的爬虫框架不要太多……
     
     
       

      不过个人觉得Jsoup最好用,最直接也很简单

      写了一个Demo,爬取笔趣网的小说,格式已过滤。

    public class CrawlText {
    
        /***
         * 获取文本
         * 
         * @param autoDownloadFile
         *            自动下载文件
         * @param Multithreading
         *            多线程 默认false
         * @param Url
         *            网站链接
         * @throws IOException
         */
        public static void getText(boolean autoDownloadFile, boolean Multithreading, String Url) throws IOException {
    
            
            String rule = "abs:href";
            
            
    
            
            List<String> urlList = new ArrayList<String>();
    
                
            Document document = Jsoup.connect(Url)
                    .timeout(4000)
                    .ignoreContentType(true)
                    .userAgent("Mozilla" to "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0)")
                    .get();
            
            System.out.println(document.toString());
            Elements urlNode = document.select("a[href$=.html]");
            
            for (Element element : urlNode) {
                urlList.add(element.attr(rule));
            }
            
            CrawTextThread crawTextThread = new CrawTextThread(urlList);
            crawTextThread.start();
    
            
        }
    
    }
    package xyz.yangchaojie.JSOUP.service;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.util.List;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    
    public class CrawTextThread extends Thread {
    
        List<String> UrlList;
    
        public CrawTextThread(List<String> urlList) {
            this.UrlList = urlList;
        }
    
        String rule = "";
        String rule_title = "h1";
        String rule_content = "content";
        
        public static String PATH = "D:\JSOUP\";
    
        /**
         * 创建文件
         * 
         * @param fileName
         * @return
         */
        public static void createFile(File fileName) throws Exception {
            try {
                if (!fileName.exists()) {
                    fileName.createNewFile();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        
        }
    
        public static void writeTxtFile(String content, File fileName) throws Exception {
            RandomAccessFile mm = null;
            FileOutputStream o = null;
            try {
                o = new FileOutputStream(fileName);
                o.write(content.getBytes("UTF-8"));
                o.close();
            } catch (Exception e) {
    
                e.printStackTrace();
            } finally {
                if (mm != null) {
                    mm.close();
                }
            }
        }
    
        @Override
        public void run() {
            currentThread().setName("一个都别跑:");
            
            String title;
            
            String content;
            
            for (String url : UrlList) {
                    try {
                        
                        Document document = Jsoup.connect(url).timeout(6000).get();
                        title = document.select("h1").toString();
                        content = document.select("#content").html();
                        
                        System.out.println("线程:"+currentThread().getName()+"爬取URL—>"+url);
                        File file = new File(PATH+title.replaceAll("<h1>", "").replaceAll("</h1>", "")+".txt");
                        createFile(file);
                        System.out.println("创建文件:"+file.getPath());
                        writeTxtFile(FileterHtml(content), file);
                        
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }
    
        }
        
        public static String FileterHtml(String str) {
            return str.replaceAll(" ", "").replaceAll("<br>", "
    ");
            
        }
    }
    public static void main( String[] args )
        {
            try {
                CrawlText.getText(true, true, "http://www.biquge.com.tw/0_66/");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    RUN:

    创建文件:D:JSOUP 关于新书的种种,在此一并交代.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83312.html
    创建文件:D:JSOUP 第一章 失意相公.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83313.html
    创建文件:D:JSOUP 第二章 吊颈秀才.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83314.html
    创建文件:D:JSOUP 第三章 秦氏族叔.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83315.html
    创建文件:D:JSOUP 第四章 无妄之灾.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83316.html
    创建文件:D:JSOUP 第五章 游衙惊梦.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83317.html
    创建文件:D:JSOUP 第六章 运蹇时乖.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83318.html
    创建文件:D:JSOUP 第七章 白手起家(上).txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83319.html
    创建文件:D:JSOUP 第八章 白手起家(下).txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83320.html
    创建文件:D:JSOUP 第九章 江南才子.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83321.html
    创建文件:D:JSOUP 第十章 才子招财.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83322.html
    创建文件:D:JSOUP 第十一章 风靡江南.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83323.html
    创建文件:D:JSOUP 第十二章 原形毕露.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83324.html
    创建文件:D:JSOUP 第十三章 杜家危局.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83325.html
    创建文件:D:JSOUP 第十四章 流年不利.txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83326.html
    创建文件:D:JSOUP 第十五章 化解危局(上).txt
    线程:一个都别跑:爬取URL—>http://www.biquge.com.tw/0_66/83327.html
    创建文件:D:JSOUP 第十六章 化解危局(中).txt
  • 相关阅读:
    微信小程序的scheme码
    微信小程序的简单总结(uni-app)
    ES7-ES11新特性
    Promise 总结
    uni-app创建项目及使用 vant-weapp
    vscode 插件整理
    el-upload 组件总结
    从输入URL到页面显示过程中发生了什么
    实验 1:Mininet 源码安装和可视化拓扑工具
    2020软件工程第一次作业
  • 原文地址:https://www.cnblogs.com/yangchaojie/p/9203876.html
Copyright © 2020-2023  润新知