Java实现一个小说采集程序的简单实例
发布于 2021-1-7|
复制链接
下面小妖就为大家带来一篇Java实现一个小说采集程序的简单实例。小妖觉得挺不错的, 现在就分享给大家,也给大家做个参考。一起跟随小妖过来看看吧
被标题吸引进来的不要骂我。只是一个简单的实现,随手写了来下载一部喜欢的小说的。示例中的小说只是示例,不是我的菜。使用了jsoup。挺好用的一个工具。有需要的话,参考下自己改吧。挺简单的,是吧。代码如下:
```java
package com.zhyea.doggie;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class Doggie {
public static void main(String[] args){
try{
File txtFile = new File("D:/无限崩坏.txt");
createTxtDoc(txtFile);
addContent(txtFile);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 向小说文件中添加内容
* @param txtFile
* 小说文件
* @throws IOException
* @throws InterruptedException
*/
private static void addContent(File txtFile) throws IOException, InterruptedException{
appendTxt(txtFile, getBookInfo("无限崩坏", "啪啪啪狂魔"));
String url = "http://www.83kxs.com/View/12/12653/{pattern}.html";
for(int i=5850686; i", "")
.replaceAll("", "")
.replaceAll("\\", "")
.replaceAll(" ", "")
.replaceAll("", NEWLINE)
+ NEWLINE;
return rtn;
}
/**
* 创建新的txt文件
* @param fullName
* 文件全名
* @return
* @throws Exception
*/
private static boolean createTxtDoc(File txtFile) throws Exception{
try{
return txtFile.createNewFile();
}catch(Exception e){
throw e;
}
}
/**
* 向txt文件中追加内容
* @param txtFile
* 要操作的txt文件
* @param content
* 要追加的内容
* @throws IOException
*/
private static void appendTxt(File txtFile, String content) throws IOException{
FileWriter writer = null;
try{
writer = new FileWriter(txtFile, true);
writer.append(content);
}finally{
if(null!=writer)writer.close();
}
}
/**
* 换行符
*/
static final String NEWLINE = System.getProperty("line.separator");
/**
* 书前的通用信息
*/
static String COMMON = "------------------------------------------------------------------" + NEWLINE
+ "--------------- 书名:{book}" + NEWLINE
+ "--------------- 作者:{author}" + NEWLINE
+ "--------------- zhyea.com" + NEWLINE
+ "------------------------------------------------------------------" + NEWLINE;
}
```