日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 批量下载小说天堂的小说

發(fā)布時間:2023/12/16 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 批量下载小说天堂的小说 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡單的小說下載工具,未進(jìn)行優(yōu)化

package com.zrc.demo.execute;import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.SocketTimeoutException; import java.util.Set; import java.util.TreeSet;import org.apache.commons.io.IOUtils; import org.jsoup.Connection; import org.jsoup.Connection.Response; import org.jsoup.HttpStatusException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; /*** 用于下載小說天堂的小說* 同時存入本地D:\\novel* @author ZRC**/ class NovelDeal extends Thread{private String cata;public NovelDeal() {}public NovelDeal(String cata) {this.cata = cata;}@Overridepublic void run(){beginWrite(cata); //開啟線程執(zhí)行的方法}public static void main(String[] args) {NovelDeal a1 = new NovelDeal("mingzhu"); //每個線程用于下載一類圖書NovelDeal a2 = new NovelDeal("ertong");NovelDeal a3 = new NovelDeal("qingchun");NovelDeal a4 = new NovelDeal("dangdai");NovelDeal a5 = new NovelDeal("gudian");NovelDeal a6 = new NovelDeal("pinglun");NovelDeal a7 = new NovelDeal("waiwen");NovelDeal a8 = new NovelDeal("sanwen");NovelDeal a9 = new NovelDeal("xuanhuan");NovelDeal a10 = new NovelDeal("chuanyue");NovelDeal a11 = new NovelDeal("wangyou");NovelDeal a12 = new NovelDeal("yanqing");NovelDeal a13 = new NovelDeal("xuanyi");NovelDeal a14 = new NovelDeal("wuxia");NovelDeal a15 = new NovelDeal("kehuan");NovelDeal a16 = new NovelDeal("renwen");NovelDeal a17 = new NovelDeal("junshi");NovelDeal a18 = new NovelDeal("zhuanji");NovelDeal a19 = new NovelDeal("lizhi");NovelDeal a20 = new NovelDeal("lishi");NovelDeal a21 = new NovelDeal("shenghuo");a1.start(); a2.start();a3.start();a4.start();a5.start();a6.start();a7.start();a8.start();a9.start();a10.start();a11.start();a12.start();a13.start();a14.start();a15.start();a16.start();a17.start();a18.start();a19.start();a20.start();a21.start();}/*** 傳入小說地址* @param address 小說地址*/public static void writeNovel(String address){Connection connect = Jsoup.connect(address).timeout(45000000).header("User-Agent", "chrome");Response execute;Document document=null;try {execute= connect.execute();document= execute.parse();} catch (IOException e) {e.printStackTrace();}String articleName = document.getElementsByTag("h1").text().replaceAll("txt下載", "");String author = document.getElementsByClass("zhaungtai").get(0).getElementsByTag("a").get(0).text();String textName = articleName+"--"+author+".txt";String catalog = document.getElementsByClass("zhaungtai").get(0).getElementsByTag("a").get(1).text();String path = "D:\\novel\\"+catalog+"\\";File pathFile = new File(path);if(!pathFile.exists()){pathFile.mkdirs();}File textFile = new File(path+textName);OutputStream textOs = null;try {textOs=new FileOutputStream(textFile);} catch (FileNotFoundException e) {e.printStackTrace();}Elements tds = document.getElementsByTag("td");Set<String> set = new TreeSet<>();for (Element ele : tds) {Elements as = ele.getElementsByTag("a");for (Element aEle : as) {set.add(aEle.attr("href"));}}StringBuilder content = new StringBuilder();for (String url : set) {Response exe = null;Document doc = null;Connection con = Jsoup.connect("http://www.xiaoshuotxt.net"+url).timeout(45000000).header("User-Agent", "chrome");try {exe = con.execute();doc = exe.parse();content.append(doc.getElementsByTag("h1").get(0).text());} catch (Exception e) {e.printStackTrace();continue;}Elements pEles = doc.getElementsByClass("zw");for (Element pEle : pEles) {content.append(pEle.toString().replaceAll("<div>", "").replaceAll("<a.*[堂]", "").replaceAll("</div>", "").replaceAll("<div class=\"zw\">", "").replaceAll("</p>", "").replaceAll("<p>", "").replaceAll("<br>", "").replaceAll("?", " ").replaceAll("[w|W|W].*[堂]", "").replaceAll("<a.*a>", "").replaceAll("</a>", "").replaceAll("小說天.*[t|T]", "").replaceAll("[w|W|W].*[o|O][m|M]", ""));}content.append("\r\n");}try {IOUtils.write(content.toString(), textOs,"GBK");textOs.close();} catch (IOException e) {e.printStackTrace();}System.out.println(catalog+"--->"+textName+"---->完成");}/*** 通過分類取得每個小說的地址* @param catalog*/public static void beginWrite(String catalog){int index = 1;while(true){String url = "http://www.xiaoshuotxt.net/"+catalog;String relUrl = "";if(index!=1){relUrl=url+"/index_"+index+".html";}else{relUrl=url;}Connection connect = Jsoup.connect(relUrl).timeout(45000000).header("User-Agent", "chrome");Elements elements;Document document;try {Response execute = connect.execute();document = execute.parse();}catch(SocketTimeoutException e){System.err.println("SocketTimeoutException-------------continue"+relUrl);e.printStackTrace();continue ;}catch(HttpStatusException e){System.err.println("HttpStatusException-------------break"+relUrl+"\n\n\n");e.printStackTrace();break;}catch (IOException e) {System.err.println("IOException-------------break"+relUrl+"\n\n\n");e.printStackTrace();break;} elements = document.getElementsByClass("bbox");for (Element element : elements) {String base = element.getElementsByTag("a").get(0).attr("href");writeNovel("http://www.xiaoshuotxt.net"+base);}index++;}} }

總結(jié)

以上是生活随笔為你收集整理的java 批量下载小说天堂的小说的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。