Java implements a simple example of a novel acquisition program _java

Source: Internet
Author: User

Be attracted by the title don't Scold me.

Just a simple implementation, conveniently written to download a favorite novel. The novel in the example is just an example, not my dish.

Use of Jsoup. A very useful tool.

If necessary, please make a reference to your own change. It's quite simple, isn't it.

The code is as follows:

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:/Infinite collapsed. txt"); 
      Createtxtdoc (txtfile);
    Addcontent (txtfile);
    }catch (Exception e) {e.printstacktrace (); }/** * Add content to the novel file * @param txtfile * @throws IOException * @throws interrupte Dexception * * private static void Addcontent (File txtfile) throws IOException, interruptedexception{Appendtxt (tx
    Tfile, Getbookinfo ("Infinite collapse", "snapped"));
    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
   /** * Set Title and author * @param bookname * @param author * author * @return * * private static string Getbookinfo (string bookname, string author) {return Common.replace (' {book} ', BookName). Re
  Place ("{author}", author); /** * Read page content * @param URL * Access Path * @return * @throws IOException/Private stat
    
    IC 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) + Newlin
    
    E
  return RTN; /** * Create a new TXT file * @param fullName * File Full name * @return * @throws Exception * * private static
    Boolean Createtxtdoc (File txtfile) throws exception{try{return Txtfile.createnewfile ();
    }catch (Exception e) {throw e; }/** * Append content to TXT file * @param txtfile * TXT file to operate * @param content * What to append * @th Rows IOException */private static void Appendtxt (File txtfile, String content) throws ioexception{FileWriter WR
    iter = null;
      try{writer = new FileWriter (txtfile, true);
    Writer.append (content);
    }finally{if (null!=writer) writer.close ();
  
  }/** * Line break/static final String newline = system.getproperty ("Line.separator"); /** * General information before the book * * static String COMMON = "------------------------------------------------------------------"+ newline +"---------------title: {book} "+ newline +"--- ------------Author: {author} "+ newline +"---------------zhyea.com "+ newline +"-----------
  
-------------------------------------------------------"+ newline; }

The above is a small series for you to bring the Java implementation of a novel collection procedures for a simple example of all the content, I hope that we support cloud-Habitat Community ~

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.