Java implementation of a novel collection program

Source: Internet
Author: User

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 ("&nbsp;", "" "). 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.