java使用jsoup下载顶点小说网

1 篇文章 0 订阅
1 篇文章 0 订阅
package bbt.servleet.request;



import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.List;



import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.nodes.Node;



public class Paqu {



public static void main(String[] args) throws IOException, InterruptedException {

query(null);

}



/**

* 查询.

* 

* @param address

* @throws IOException

* @throws InterruptedException 

*/

public static void query(String address) throws IOException, InterruptedException {

String defaultAddress = "http://www.23us.so/files/article/html/12/12920/4999702.html"; //默认地址

if (null != address) {

defaultAddress = address;

}

Document document = Jsoup.connect(defaultAddress).get(); // 获取链接

Element element = document.getElementById("footlink");  //取下一页,使用前台js

List<Node> nodeList = element.childNodes();

boolean isNext = false;

String nextaddress = null;

for (Node node : nodeList) {

if (node.toString().contains("下一页")) {

String pageAddress = node.attr("abs:href"); //绝对url封装

nextaddress = pageAddress;

isNext = true;

}

}

uploadPage(document);

if (isNext) {

Thread.sleep(2000); // 防止请求过繁忙,封锁ip;

query(nextaddress);

}

}



/**

* 下载.

* 

* @param document

*            文件

* @throws IOException

*/

public static void uploadPage(Document document) throws IOException {

String bookTitle = document.title().replace("?", "");//截取特殊字符

String bookContent = document.getElementById("contents").html();

byte[] byteContent = bookContent.getBytes(); //获取小说正体内容



File file = new File("/Users/apple-w/Downloads/xiaoqiang/book");

if (!file.exists()) {

file.mkdirs();

}

String path = "/Users/apple-w/Downloads/xiaoqiang/book" + "/" + bookTitle.split("-")[0] + ".html";

File outputFile = new File(path);

FileOutputStream fos = new FileOutputStream(outputFile);

fos.write(byteContent);

fos.close();

System.err.println(path);

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值