WebCrawler Java小爬爬 从入门到放弃 第五章

如果你要写一个灰常灰常简单的的爬虫 第一章+第二章 就结束了
因为博主爬的这个稍稍有点复杂 :) 我没有开玩笑 理解了 就挺简单


博主要爬的是网站是这个:http://www.mzhu8.com/mulu/17/1.html
!!!强烈建议用个插件 或者净网大师 否则会弹出来十万个网页 :)
我们在考虑选材时,觉得国外的名著和网络小说,很难分析出写作风格。
所以我们选择了近代文学,这是一个有三级页面的网页。
我们已经把数据库、网页提取、正则表达式做完了。
接下来要做的是,细化下把有效信息写入本地的类。
先在D盘建立一个文件夹FamousBook 用于存储我们爬到的书籍
先思考一个问题:文件目录形式是怎样的?
….
…..
……
思考良久 建立目录结构:著作文件夹->作者文件夹->书籍

然后开始coding….

import java.io.*;
/**
 * 
 * establish book storage structure and write
 * book into file 
 * @author Mr.Dragon
 * @Time 2016-9-20
 * @version   1.0
 *
 */
public class BookWriter {
    private static String filepath;
    public static boolean createAuthorCatlog(String authorname){  
        //if author not existed,create author's folder 
        filepath = null;
        filepath = FilePath.filepath + authorname + "\\";
        File file = new File(filepath); 
        if(!file.exists())file.mkdirs();
        if(file.exists())return true;
        return false;       
    }

    public static void writeIntoFile(String book,String author,
            String bookcontent) {       
        File file ;
        if(createAuthorCatlog(author)){
            O.p(author+" folder has successfully established.");
        }else{
            O.p(author+" folder established failed.");
        }
        filepath += book + ".txt";
        if(null == FamousBookLocalInfo.getStatus(book)){
            //if book has not saved,save the book information
            FamousBookLocalInfo.insertBookInfo(book, author);
        }       
        //System.out.println(FamousBookLocalInfo.getStatus(book)); 
        //testing book's status.
        if(FamousBookLocalInfo.getStatus(book).equals("new")){ 
            //FamousBookLocalInfo.updateWordsnumAndStatus(num, status);
            file = new File(filepath);
            try {
                file.createNewFile();
                FileWriter fileWriter = new FileWriter(file);
                String synopsis = "Bookname:"+book+"\r\nAuthor:"+ author +"\r\n";
                fileWriter.write(synopsis);  
                fileWriter.close();
                FamousBookLocalInfo.updateWordsnumAndStatus(0);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                O.p("Create "+book+"'s file falied");
            }
        }
        //testing file path
        System.out.println(filepath);
        file = new File(filepath);
        FileWriter fileWriter;
        try {
            file.createNewFile();
            fileWriter = new FileWriter(file,true);
            fileWriter.write(bookcontent);

            FamousBookLocalInfo.updateWordsnumAndStatus(bookcontent.length());
            fileWriter.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }  
}

写一个静态方法createAuthorCatlog()返回布尔值 表示创建成功与否
本来是想去访问数据库 看作者是否存在 然后决定是否在本地建立对应作者的文件夹
最后发现直接在本地查找更方便,异常错误发生会更少

再写了一个writeIntoFile()写入到本地
其中O是我单路写的一个类 只有一个静态方法p
主要是因为System.out.print太长了 太麻烦

/**
 * 
 * simplified system out 
 * @author Mr.Dragon
 * 
 */
public class O {
    public static void p(String s){
        System.out.println(s);
    }
}

这个FamousBookLocalInfo类是查询本地存储信息的一个类 可以根据其方法理解意思 明天贴出这个类的实现
游戏开了….

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值