在E小说里面下载

@Test
    public void xzxs(){
        try {
            writeXs("/shashen/1195968","C:/Users/Desktop/test.txt");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void writeXs(String nextUrl,String path) throws Exception{
        String start = "http://www.exiaoshuo.com";
        String url = start + nextUrl;
        Document doc = Jsoup.connect(url).get();
        String title = doc.title();
        Elements body = doc.select("div[id^=booktext]");
        Elements a = doc.select("div[class^=book_middle_text_next]");
        String context = body.text();
        OutputStreamWriter writer=new OutputStreamWriter(new
                FileOutputStream(new File(path),true));
        StringBuffer buffer=new StringBuffer();
        buffer.append(title);
        buffer.append("\r\n");
        for (int i = 0; i < context.length(); i++) {
            buffer.append(context.charAt(i));
            if (i!=0&&i%80==0) {
                buffer.append("\r\n");
            }
        }
        writer.write(buffer.toString());
        writer.flush();
        writer.close();
        String Nexturl = getNextUrl(a.html());
        if (Nexturl!=null) {
            writeXs(Nexturl,path);
        }
    }
    public String getNextUrl(String html){
        String end = "../";
        String tmp = StringUtils.substringBetween(html,"返回目录", "下一章");
        String url  = StringUtils.substringBetween(tmp, "href=\"", "\">");
        if (StringUtils.endsWithIgnoreCase(url, end)
                ||StringUtils.isBlank(url)) {
            return null;
        }
        return url;
    }

你可能感兴趣的:(下载小说)