零基礎(chǔ)寫Java知乎爬蟲之將抓取的內(nèi)容存儲到本地
說到Java的本地存儲,肯定使用IO流進行操作。
首先,我們需要一個創(chuàng)建文件的函數(shù)createNewFile:
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;
}
然后,我們需要一個寫入文件的函數(shù):
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;
}
我們把這兩個函數(shù)封裝到一個FileReaderWriter.java文件中以便后續(xù)使用。
接著我們回到知乎爬蟲中。
我們需要給知乎的Zhihu封裝類加個函數(shù),用來格式化寫入到本地時的排版。
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改成
// 寫入本地
for (Zhihu zhihu : myZhihu) {
FileReaderWriter.writeIntoFile(zhihu.writeString(),
"D:/知乎_編輯推薦.txt", true);
}
運行,便可以看到本來在控制臺看到的內(nèi)容已經(jīng)被寫到了本地的txt文件里:

大體一看沒什么問題,仔細看看發(fā)現(xiàn)問題:存在太多的html標簽,主要是<b>和<br>。
我們可以在輸出的時候?qū)@些標記進行處理。
先把<br>換成io流里面的\r\n,再把所有的html標簽都刪除,這樣看起來便會清晰很多。
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標簽進行篩選
result = result.replaceAll("<br>", "\r\n");
result = result.replaceAll("<.*?>", "");
return result;
}
這里的replaceAll函數(shù)可以使用正則,于是所有的<>標簽在最后就都被刪除了。
相關(guān)文章
SpringBoot利用@Retryable注解實現(xiàn)接口重試
本文主要介紹了springboot如何利用@Retryable注解實現(xiàn)接口重試功能,文中示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Java實現(xiàn)文件或文件夾的復(fù)制到指定目錄實例
本篇文章主要介紹了Java實現(xiàn)文件或文件夾的復(fù)制到指定目錄實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
spring框架配置實體類復(fù)雜屬性注入xml文件過程詳解
這篇文章主要介紹了spring框架配置實體類復(fù)雜屬性注入xml文件過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09
深入探究 spring-boot-starter-parent的作用
這篇文章主要介紹了spring-boot-starter-parent的作用詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,感興趣的小伙伴可以跟著小編一起來學(xué)習(xí)一下2023-05-05
Java同步框架AbstractQueuedSynchronizer詳解
本篇文章主要介紹了Java同步框架AbstractQueuedSynchronizer詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Java前后端分離的在線點餐系統(tǒng)實現(xiàn)詳解
這是一個基于SpringBoot+Vue框架開發(fā)的在線點餐系統(tǒng)。首先,這是一個前后端分離的項目。具有一個在線點餐系統(tǒng)該有的所有功能,感興趣的朋友快來看看吧2022-01-01

