零基础写Java知乎爬虫之将抓取的内容存储到本地
作者:hebedich 时间:2022-08-16 20:25:45
说到Java的本地存储,肯定使用IO流进行操作。
首先,我们需要一个创建文件的函数createNewFile:
public static boolean createNewFile(String filePath) {
boolean isSuccess = true;
// 如有则将"\\"转为"/",没有则不产生任何变化
String filePathTurn = filePath.replaceAll("\\\\", "/");
// 先过滤掉文件名
int index = filePathTurn.lastIndexOf("/");
String dir = filePathTurn.substring(0, index);
// 再创建文件夹
File fileDir = new File(dir);
isSuccess = fileDir.mkdirs();
// 创建文件
File file = new File(filePathTurn);
try {
isSuccess = file.createNewFile();
} catch (IOException e) {
isSuccess = false;
e.printStackTrace();
}
return isSuccess;
}
然后,我们需要一个写入文件的函数:
public static boolean writeIntoFile(String content, String filePath,
boolean isAppend) {
boolean isSuccess = true;
// 先过滤掉文件名
int index = filePath.lastIndexOf("/");
String dir = filePath.substring(0, index);
// 创建除文件的路径
File fileDir = new File(dir);
fileDir.mkdirs();
// 再创建路径下的文件
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;
}
我们把这两个函数封装到一个FileReaderWriter.java文件中以便后续使用。
接着我们回到知乎爬虫中。
我们需要给知乎的Zhihu封装类加个函数,用来格式化写入到本地时的排版。
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);
}
运行,便可以看到本来在控制台看到的内容已经被写到了本地的txt文件里:
大体一看没什么问题,仔细看看发现问题:存在太多的html标签,主要是<b>和<br>。
我们可以在输出的时候对这些标记进行处理。
先把<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函数可以使用正则,于是所有的<>标签在最后就都被删除了。
标签:Java,爬虫,知乎
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mybatis plus新增(insert)数据获取主键id的问题
2023-08-09 10:50:52
Java jvm中Code Cache案例详解
2022-02-04 17:00:53
java日期格式化SimpleDateFormat的使用详解
2023-08-25 03:22:15
![](https://img.aspxhome.com/file/2023/0/58460_0s.png)
Springboot实现Shiro整合JWT的示例代码
2022-01-30 02:58:32
![](https://img.aspxhome.com/file/2023/1/65601_0s.png)
解决Android Studio安装后运行出错dose not...和Internal error...
2023-04-24 11:54:42
![](https://img.aspxhome.com/file/2023/9/64919_0s.png)
Android之使用Bundle进行IPC详解
2023-09-27 22:44:56
Android Flutter中Offstage组件的使用教程详解
2023-08-20 01:01:44
java中TESTful架构原理分析
2022-03-02 21:12:10
![](https://img.aspxhome.com/file/2023/9/61659_0s.jpg)
SpringBoot自动配置深入探究实现原理
2023-08-06 09:59:58
![](https://img.aspxhome.com/file/2023/9/62059_0s.png)
JVM类加载,垃圾回收
2022-08-16 02:44:36
![](https://img.aspxhome.com/file/2023/1/61771_0s.png)
详解Java双轴快速排序算法
2023-10-05 15:50:14
![](https://img.aspxhome.com/file/2023/6/69316_0s.jpg)
JAVA实现单例模式的四种方法和一些特点
2023-11-02 05:38:20
JavaCV实现照片马赛克效果
2023-04-27 15:55:14
![](https://img.aspxhome.com/file/2023/4/66624_0s.jpg)
java static块和构造函数的实例详解
2021-10-04 07:30:17
Java手写线程池的实现方法
2023-10-30 12:50:03
![](https://img.aspxhome.com/file/2023/1/58701_0s.jpg)
android图片处理 让图片变成圆形
2023-10-03 00:47:30
![](https://img.aspxhome.com/file/2023/6/83536_0s.jpg)
Java 添加和删除PDF图层的示例代码
2022-09-03 00:29:01
![](https://img.aspxhome.com/file/2023/2/66512_0s.gif)
java 线程方法join简单用法实例总结
2022-10-26 19:27:38
spring cloud Ribbon用法及原理解析
2021-11-28 15:27:21
JavaWeb项目Servlet无法访问问题解决
2022-10-10 03:33:52