欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

零基礎(chǔ)寫Java知乎爬蟲之將抓取的內(nèi)容存儲(chǔ)到本地

 更新時(shí)間:2014年11月07日 11:19:37   投稿:hebedich  
上一回我們說到了如何把知乎的某些內(nèi)容爬取出來,那么這一回我們就說說怎么把這些內(nèi)容存儲(chǔ)到本地吧。

說到Java的本地存儲(chǔ),肯定使用IO流進(jìn)行操作。
首先,我們需要一個(gè)創(chuàng)建文件的函數(shù)createNewFile:

復(fù)制代碼 代碼如下:

public static boolean createNewFile(String filePath) { 
        boolean isSuccess = true; 
        // 如有則將"\\"轉(zhuǎn)為"/",沒有則不產(chǎn)生任何變化 
        String filePathTurn = filePath.replaceAll("\\\\", "/"); 
        // 先過濾掉文件名 
        int index = filePathTurn.lastIndexOf("/"); 
        String dir = filePathTurn.substring(0, index); 
        // 再創(chuàng)建文件夾 
        File fileDir = new File(dir); 
        isSuccess = fileDir.mkdirs(); 
        // 創(chuàng)建文件 
        File file = new File(filePathTurn); 
        try { 
            isSuccess = file.createNewFile(); 
        } catch (IOException e) { 
            isSuccess = false; 
            e.printStackTrace(); 
        } 
        return isSuccess; 
    } 

然后,我們需要一個(gè)寫入文件的函數(shù):

復(fù)制代碼 代碼如下:

public static boolean writeIntoFile(String content, String filePath, 
            boolean isAppend) { 
        boolean isSuccess = true; 
        // 先過濾掉文件名 
        int index = filePath.lastIndexOf("/"); 
        String dir = filePath.substring(0, index); 
        // 創(chuàng)建除文件的路徑 
        File fileDir = new File(dir); 
        fileDir.mkdirs(); 
        // 再創(chuàng)建路徑下的文件 
        File file = null; 
        try { 
            file = new File(filePath); 
            file.createNewFile(); 
        } catch (IOException e) { 
            isSuccess = false; 
            e.printStackTrace(); 
        } 
        // 寫入文件 
        FileWriter fileWriter = null; 
        try { 
            fileWriter = new FileWriter(file, isAppend); 
            fileWriter.write(content); 
            fileWriter.flush(); 
        } catch (IOException e) { 
            isSuccess = false; 
            e.printStackTrace(); 
        } finally { 
            try { 
                if (fileWriter != null) 
                    fileWriter.close(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 
 
        return isSuccess; 
    } 

我們把這兩個(gè)函數(shù)封裝到一個(gè)FileReaderWriter.java文件中以便后續(xù)使用。
接著我們回到知乎爬蟲中。
我們需要給知乎的Zhihu封裝類加個(gè)函數(shù),用來格式化寫入到本地時(shí)的排版。

復(fù)制代碼 代碼如下:

public String writeString() { 
        String result = ""; 
        result += "問題:" + question + "\r\n"; 
        result += "描述:" + questionDescription + "\r\n"; 
        result += "鏈接:" + zhihuUrl + "\r\n"; 
        for (int i = 0; i < answers.size(); i++) { 
            result += "回答" + i + ":" + answers.get(i) + "\r\n"; 
        } 
        result += "\r\n\r\n"; 
        return result; 

OK,這樣就差不多了,接下來吧mian方法中的System.out.println改成

復(fù)制代碼 代碼如下:

// 寫入本地 
        for (Zhihu zhihu : myZhihu) { 
            FileReaderWriter.writeIntoFile(zhihu.writeString(), 
                    "D:/知乎_編輯推薦.txt", true); 
        } 

運(yùn)行,便可以看到本來在控制臺(tái)看到的內(nèi)容已經(jīng)被寫到了本地的txt文件里:

大體一看沒什么問題,仔細(xì)看看發(fā)現(xiàn)問題:存在太多的html標(biāo)簽,主要是<b>和<br>。
我們可以在輸出的時(shí)候?qū)@些標(biāo)記進(jìn)行處理。
先把<br>換成io流里面的\r\n,再把所有的html標(biāo)簽都刪除,這樣看起來便會(huì)清晰很多。

復(fù)制代碼 代碼如下:

public String writeString() { 
    // 拼接寫入本地的字符串 
    String result = ""; 
    result += "問題:" + question + "\r\n"; 
    result += "描述:" + questionDescription + "\r\n"; 
    result += "鏈接:" + zhihuUrl + "\r\n"; 
    for (int i = 0; i < answers.size(); i++) { 
        result += "回答" + i + ":" + answers.get(i) + "\r\n\r\n"; 
    } 
    result += "\r\n\r\n\r\n\r\n"; 
    // 將其中的html標(biāo)簽進(jìn)行篩選 
    result = result.replaceAll("<br>", "\r\n"); 
    result = result.replaceAll("<.*?>", ""); 
    return result; 

這里的replaceAll函數(shù)可以使用正則,于是所有的<>標(biāo)簽在最后就都被刪除了。

相關(guān)文章

最新評(píng)論