软件园学生在线

  • {{ item.name }}
  • 2023试用期

登录与注册

【二】李文才

  • wencai
  • 2022-10-23
  • 0

代码部分

package org.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Safelist;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.Scanner;
public class Worm {

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

    String menuUrl = "https://www.aixiaxsw.com/105/105503/";
    String menu1 = "https://www.aixiaxsw.com";
    final String fileAddr = "./";
    Scanner sc = new Scanner(System.in);
    System.out.println("输入1则根据接下来提供的url自由爬取,输入2则爬取示例小说《天灾之龙》");
    if (sc.nextInt() == 1) {
        System.out.println("请输入爱下书小说网的URL,例如:https://www.aixiaxsw.com/105/105503/");
        menuUrl = sc.next();
    }
    Document document = null;
    try {
        document = Jsoup.connect(menuUrl).get();
    } catch (IOException ewww) {
        ewww.printStackTrace();
    }
    String title = document.selectFirst("h1").text();
    String writer = document.selectFirst("p").text();
    System.out.println("开始爬取:" + title);
    Elements menu = document.body().select("dl dd");
    Elements as = menu.select("a[href]");

    System.out.println("小说将保存在:" + fileAddr + title + ".md   中");

    File file = new File(fileAddr + title + ".md");
    OutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    fileOut.write(("# "+title + "            " + "\n\n").getBytes());
    fileOut.write((  writer + "\n\n").getBytes());
    int count = 1;
    for (Element a : as) {
        count++;
        if (count > 10) {
            String chapterName = a.text();
            String subLink = a.attr("href");
            fileOut.write(("\n" + "[" + chapterName + "]" + "(" + menu1 + subLink + ")").getBytes());
        }
    }
    int i = 1;
    for (Element a : as) {
        i++;
        if (i > 10) {
            String subLink = a.attr("href");
            String chapterName = a.text();
            System.out.println("正在爬取:" + chapterName);
            Document chapter = null;
            try {
                chapter = Jsoup.connect(menu1 + subLink).timeout(10000).get();
            } catch (IOException ewww) {
                ewww.printStackTrace();
            }
            Element chapterContent = chapter.selectFirst("#content");
            String baseContent = Jsoup.clean(chapterContent.toString(), "", Safelist.none(), new Document.OutputSettings().prettyPrint(false));
            String newText = baseContent.replaceAll("\\s{2,}", "\n");
            String trueContent = newText.replaceFirst("\n", "").trim();
            fileOut.write(("\n\n" + "### " + chapterName + "\n\n" + trueContent).getBytes());
        }
    }

    System.out.println("爬取完成!已保存在根目录。");
    fileOut.close();

}

}

爬虫代码感言

  • 真的大部分代码都不明白真的好难好难,,,这份代码也是在同学和学长的帮助下完成的。/哭
  • 优点:
    1.这个代码可以实现爱下小说网站里面所有小说的自由爬取,还有一部爬取实例。

    2.用户交互界面较为友善。

    3.保存在根目录后实现了链接到原网页和简单排版,不会一行字在一起。

  • 缺点:
    1.没有实现用户自由选择保存路径。

    2.没有实现良好排版。

    3.小说网站有些局限。

    4.zxgg教的检查用户输入格式对不对也没有用上,从gg讲的还有网上的一部分大概学会了start with的方法,但是看着这个有点陌生的程序有些不知道该插入到哪里。

    5.终归不是自己的代码,该了一些体验方面的事情之后程序就没法跑了,也不知道哪里出错了,就不敢改,又改回来了。

wencai
wencai
© 2025 软件园学生在线
Theme by Wing