Be attracted by the title don't Scold me.
Just a simple implementation, readily written to download a favorite novel. The novel in the example is just an example, not my dish.
The Jsoup is used. A tool that works very well.
If you need to, you can change it with your own reference. It's quite simple, isn't it.
The code is as follows:
PackageCom.zhyea.doggie;ImportJava.io.File;ImportJava.io.FileWriter;Importjava.io.IOException;ImportOrg.jsoup.Jsoup;Importorg.jsoup.nodes.Document;Importorg.jsoup.select.Elements; Public classDoggie { Public Static voidMain (string[] args) {Try{File txtfile=NewFile ("d:/infinite collapse. txt"); Createtxtdoc (txtfile); Addcontent (txtfile); }Catch(Exception e) {e.printstacktrace (); } } /*** Add content to a novel file *@paramtxtfile * Fiction file *@throwsIOException *@throwsinterruptedexception*/ Private Static voidAddcontent (File txtfile)throwsIOException, interruptedexception{appendtxt (txtfile, Getbookinfo ("Infinite collapse", "popping the devil")); String URL= "Http://www.83kxs.com/view/12/12653/{pattern}.html"; for(inti=5850686; i<=5945501; i++){ Try{String tmp= Url.replace ("{pattern}", i+ ""); Appendtxt (txtfile, Getpagecontent (TMP)); }Catch(Exception e) {e.printstacktrace (); Continue; } } } /*** Set the title and author *@paramBookName * Title *@paramAuthor * author *@return */ Private Staticstring Getbookinfo (String bookname, string author) {returnCommon.replace ("{book}", BookName). Replace ("{author}", author); } /*** Read Page contents *@paramURL * Access Path *@return * @throwsIOException*/ Private StaticString getpagecontent (string url)throwsioexception{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; returnRtn; } /*** Create a new TXT file *@paramFullName * Full name of file *@return * @throwsException*/ Private Static BooleanCreatetxtdoc (File txtfile)throwsexception{Try{ returnTxtfile.createnewfile (); }Catch(Exception e) {Throwe; } } /*** Append content to TXT file *@paramtxtfile * TXT file to operate *@paramcontent * What to append *@throwsIOException*/ Private Static voidAppendtxt (File txtfile, String content)throwsioexception{FileWriter writer=NULL; Try{writer=NewFileWriter (Txtfile,true); Writer.append (content); }finally{ if(NULL!=writer) writer.close (); } } /*** Line Break*/ Static FinalString NEWLINE = System.getproperty ("Line.separator"); /*** General information before the book*/ StaticString COMMON = "------------------------------------------------------------------" +NEWLINE+ "---------------title: {Book}" +NEWLINE+ "---------------{author}" +NEWLINE+ "---------------zhyea.com" +NEWLINE+ "------------------------------------------------------------------" +NEWLINE; }
Java implementation of a novel collection program