Java實(shí)現(xiàn)一個(gè)小說采集程序的簡(jiǎn)單實(shí)例
被標(biāo)題吸引進(jìn)來(lái)的不要罵我。
只是一個(gè)簡(jiǎn)單的實(shí)現(xiàn),隨手寫了來(lái)下載一部喜歡的小說的。示例中的小說只是示例,不是我的菜。
使用了jsoup。挺好用的一個(gè)工具。
有需要的話,參考下自己改吧。挺簡(jiǎn)單的,是吧。
代碼如下:
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:/無(wú)限崩壞.txt");
createTxtDoc(txtFile);
addContent(txtFile);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 向小說文件中添加內(nèi)容
* @param txtFile
* 小說文件
* @throws IOException
* @throws InterruptedException
*/
private static void addContent(File txtFile) throws IOException, InterruptedException{
appendTxt(txtFile, getBookInfo("無(wú)限崩壞", "啪啪啪狂魔"));
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;
}
}
}
/**
* 設(shè)置書名和作者
* @param bookName
* 書名
* @param author
* 作者
* @return
*/
private static String getBookInfo(String bookName, String author){
return COMMON.replace("{book}", bookName).replace("{author}", author);
}
/**
* 讀取頁(yè)面內(nèi)容
* @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;
}
/**
* 創(chuàng)建新的txt文件
* @param fullName
* 文件全名
* @return
* @throws Exception
*/
private static boolean createTxtDoc(File txtFile) throws Exception{
try{
return txtFile.createNewFile();
}catch(Exception e){
throw e;
}
}
/**
* 向txt文件中追加內(nèi)容
* @param txtFile
* 要操作的txt文件
* @param content
* 要追加的內(nèi)容
* @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;
}
以上就是小編為大家?guī)?lái)的Java實(shí)現(xiàn)一個(gè)小說采集程序的簡(jiǎn)單實(shí)例全部?jī)?nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
Maven項(xiàng)src/main/java目錄下配置文件無(wú)法被導(dǎo)出或者生效的問題和處理方案
這篇文章主要介紹了Maven項(xiàng)src/main/java目錄下配置文件無(wú)法被導(dǎo)出或者生效的問題和處理方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Spring?cloud如何實(shí)現(xiàn)FeignClient指定Zone調(diào)用
這篇文章主要介紹了Spring?cloud如何實(shí)現(xiàn)FeignClient指定Zone調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行sql腳本的方法步驟
本文主要介紹了SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行sql腳本的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
AgileBoot?項(xiàng)目?jī)?nèi)統(tǒng)一的錯(cuò)誤碼設(shè)計(jì)分析
這篇文章主要為大家介紹了AgileBoot?項(xiàng)目?jī)?nèi)統(tǒng)一的錯(cuò)誤碼設(shè)計(jì)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏
這篇文章主要介紹了Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
SpringBoot2零基礎(chǔ)到精通之自動(dòng)配置底層分析及小技巧
SpringBoot是一種整合Spring技術(shù)棧的方式(或者說是框架),同時(shí)也是簡(jiǎn)化Spring的一種快速開發(fā)的腳手架,本篇讓我們一起學(xué)習(xí)自動(dòng)配置的底層分析與一些開發(fā)中的小技巧2022-03-03
Java實(shí)現(xiàn)鼠標(biāo)模擬與鍵盤映射
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)鼠標(biāo)模擬與鍵盤映射,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Java web基礎(chǔ)學(xué)習(xí)之開發(fā)環(huán)境篇(詳解)
下面小編就為大家?guī)?lái)一篇Java web基礎(chǔ)學(xué)習(xí)之開發(fā)環(huán)境篇(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-08-08

