Java实现一个小说采集程序的简单实例
时间:2021-05-27 08:42:43|栏目:JAVA代码|点击: 次
被标题吸引进来的不要骂我。
只是一个简单的实现,随手写了来下载一部喜欢的小说的。示例中的小说只是示例,不是我的菜。
使用了jsoup。挺好用的一个工具。
有需要的话,参考下自己改吧。挺简单的,是吧。
代码如下:
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<=5945501; i++){
try{
String tmp = url.replace("{pattern}", i+"");
appendTxt(txtFile, getPageContent(tmp));
}catch(Exception e){
e.printStackTrace();
continue;
}
}
}
/**
* 设置书名和作者
* @param bookName
* 书名
* @param author
* 作者
* @return
*/
private static String getBookInfo(String bookName, String author){
return COMMON.replace("{book}", bookName).replace("{author}", author);
}
/**
* 读取页面内容
* @param url
* 访问路径
* @return
* @throws IOException
*/
private static String getPageContent(String url) throws IOException{
String rtn = null;
Document doc = Jsoup.connect(url).get();
Elements content = doc.select(".text p");
Elements title = doc.select("#title");
System.out.println(title.text());
content.select("font").remove();
content.select("script").remove();
content.select("ins").remove();
content.select("a").remove();
rtn = title.text() + NEWLINE
+ content.html().replaceAll("<p>", "")
.replaceAll("</p>", "")
.replaceAll("\\<!--(.+)--\\>", "")
.replaceAll(" ", "")
.replaceAll("<br>", 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;
}
上一篇:Java 定时器的使用示例
栏 目:JAVA代码
本文标题:Java实现一个小说采集程序的简单实例
本文地址:http://www.codeinn.net/misctech/129666.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




