当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现一个小说采集程序的简单实例

Java实现一个小说采集程序的简单实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

被标题吸引进来的不要骂我。

只是一个简单的实现,随手写了来下载一部喜欢的小说的。示例中的小说只是示例,不是我的菜。

使用了jsoup。挺好用的一个工具。

有需要的话,参考下自己改吧。挺简单的,是吧。

代码如下:

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:/无限崩坏.txt");
      createtxtdoc(txtfile); 
      addcontent(txtfile);
    }catch(exception e){
      e.printstacktrace();
    }
      
  }
  
  /**
   * 向小说文件中添加内容
   * @param txtfile
   *       小说文件
   * @throws ioexception
   * @throws interruptedexception
   */
  private static void addcontent(file txtfile) throws ioexception, interruptedexception{
    appendtxt(txtfile, getbookinfo("无限崩坏", "啪啪啪狂魔"));
    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;
      }
    }
  }
    
  /**
   * 设置书名和作者
   * @param bookname
   *         书名
   * @param author
   *         作者
   * @return
   */
  private static string getbookinfo(string bookname, string author){
    return common.replace("{book}", bookname).replace("{author}", author);
  }  
  
  /**
   * 读取页面内容
   * @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;
  }
  
  /**
   * 创建新的txt文件
   * @param fullname
   *       文件全名
   * @return
   * @throws exception
   */
  private static boolean createtxtdoc(file txtfile) throws exception{
    try{
      return txtfile.createnewfile();
    }catch(exception e){
      throw e;
    }
  }
  
  
  /**
   * 向txt文件中追加内容
   * @param txtfile
   *       要操作的txt文件
   * @param content
   *       要追加的内容
   * @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;
  
}

以上就是小编为大家带来的java实现一个小说采集程序的简单实例全部内容了,希望大家多多支持移动技术网~

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网