import\u0020javafx.application.Application;import\u0020javafx.application.Platform;import\u0020javafx.concurrent.Task;import\u0020javafx.geometry.Insets;import\u0020javafx.geometry.Pos;import\u0020javafx.scene.Scene;import\u0020javafx.scene.control.;import\u0020javafx.scene.layout.;import\u0020javafx.stage.Stage;import\u0020org.jsoup.Jsoup;import\u0020org.jsoup.nodes.Document;import\u0020org.jsoup.select.Elements;import\u0020org.openqa.selenium.By;import\u0020org.openqa.selenium.Keys;import\u0020org.openqa.selenium.WebDriver;import\u0020org.openqa.selenium.WebElement;import\u0020org.openqa.selenium.chrome.ChromeDriver;import\u0020org.openqa.selenium.chrome.ChromeOptions;import\u0020org.openqa.selenium.support.ui.ExpectedConditions;import\u0020org.openqa.selenium.support.ui.WebDriverWait;import\u0020java.io.BufferedWriter;import\u0020java.io.File;import\u0020java.io.FileWriter;import\u0020java.io.IOException;import\u0020java.util.HashMap;import\u0020java.util.List;import\u0020java.util.Map;import\u0020java.util.stream.Collectors;public\u0020class\u0020NovelDownloader\u0020extends\u0020Application\u0020{\u0020private\u0020Map<String,\u0020String>\u0020chapters\u0020=\u0020new\u0020HashMap<>();\u0020private\u0020TextField\u0020novelNameInput;private\u0020CheckBox\u0020saveCheckBox;private\u0020ListView\u0020chapterList;private\u0020TextArea\u0020chapterText;private\u0020WebDriver\u0020driver;public\u0020static\u0020void\u0020main(String[]\u0020args)\u0020{\u0020launch(args);}\u0020@Override\u0020public\u0020void\u0020start(Stage\u0020primaryStage)\u0020{\u0020primaryStage.setTitle("小说下载器");novelNameInput\u0020=\u0020new\u0020TextField();saveCheckBox\u0020=\u0020new\u0020CheckBox("是否下载小说内容到当前目录下");Button\u0020searchButton\u0020=\u0020new\u0020Button("获取");chapterList\u0020=\u0020new\u0020ListView<>();chapterText\u0020=\u0020new\u0020TextArea();VBox\u0020root\u0020=\u0020new\u0020VBox(10);root.setPadding(new\u0020Insets(10));HBox\u0020inputBox\u0020=\u0020new\u0020HBox(10);HBox.setHgrow(novelNameInput,\u0020Priority.ALWAYS);inputBox.getChildren().addAll(novelNameInput,\u0020saveCheckBox,\u0020searchButton);HBox\u0020contentBox\u0020=\u0020new\u0020HBox(10);VBox.setVgrow(contentBox,\u0020Priority.ALWAYS);VBox.setVgrow(chapterText,\u0020Priority.ALWAYS);contentBox.getChildren().addAll(chapterList,\u0020chapterText);root.getChildren().addAll(new\u0020Label("请输入小说名称:"),\u0020inputBox,\u0020contentBox);searchButton.setOnAction(event\u0020->\u0020{\u0020String\u0020novelName\u0020=\u0020novelNameInput.getText();if\u0020(!novelName.isEmpty())\u0020{\u0020searchNovelAndScrape(novelName);}\u0020else\u0020{\u0020showAlert("警告",\u0020"请输入小说名称",\u0020Alert.AlertType.WARNING);}\u0020});chapterList.getSelectionModel().selectedItemProperty().addListener((observable,\u0020oldValue,\u0020newValue)\u0020->\u0020{\u0020if\u0020(newValue\u0020!=\u0020null)\u0020{\u0020showChapterContent(newValue);}\u0020});Scene\u0020scene\u0020=\u0020new\u0020Scene(root,\u0020800,\u0020600);scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());primaryStage.setScene(scene);primaryStage.show();}\u0020public\u0020void\u0020searchNovelAndScrape(String\u0020novelName)\u0020{\u0020Task\u0020task\u0020=\u0020new\u0020Task()\u0020{\u0020@Override\u0020protected\u0020Void\u0020call()\u0020throws\u0020Exception\u0020{\u0020try\u0020{\u0020setupChromeDriver();driver.get("https://www.bige3.cc/");WebElement\u0020searchInput\u0020=\u0020driver.findElement(By.xpath("/html/body/div[4]/div[1]/div[2]/form/input[1]"));searchInput.sendKeys(novelName);searchInput.sendKeys(Keys.ENTER);WebDriverWait\u0020wait\u0020=\u0020new\u0020WebDriverWait(driver,\u002010);wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[5]/div/div/div/div/div[2]/h4/a")));List\u0020elements\u0020=\u0020driver.findElements(By.xpath("/html/body/div[5]/div/div/div/div/div[2]/h4/a"));if\u0020(elements.isEmpty())\u0020{\u0020showAlert("提示",\u0020"没有找到相关小说",\u0020Alert.AlertType.INFORMATION);return\u0020null;}for\u0020(WebElement\u0020element\u0020:\u0020elements)\u0020{\u0020element.click();driver.switchTo().window(driver.getWindowHandles().stream().reduce((first,\u0020second)\u0020->\u0020second).orElse(null));wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//[@class='listmain']/dl/dd/a")));List\u0020chapterElements\u0020=\u0020driver.findElements(By.xpath("//[@class='listmain']/dl/dd/a"));for\u0020(WebElement\u0020chapter\u0020:\u0020chapterElements)\u0020{\u0020chapter.click();while\u0020(true)\u0020{\u0020wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//[@id='read']/div[5]/div[3]/h1")));WebElement\u0020chapterTitleElement\u0020=\u0020driver.findElement(By.xpath("//[@id='read']/div[5]/div[3]/h1"));WebElement\u0020chapterContentElement\u0020=\u0020driver.findElement(By.xpath("//*[@id='chaptercontent']"));String\u0020chapterTitle\u0020=\u0020chapterTitleElement.getText().replace("、",\u0020"");String\u0020chapterContent\u0020=\u0020chapterContentElement.getAttribute("innerHTML").replaceAll("无弹窗,更新快,免费阅读!",\u0020"").replaceAll("请收藏本站:https://www.bige3.cc。笔趣阁手机版:https://m.bige3.cc",\u0020"").replaceAll("『点此报错』『加入书签』",\u0020"");if\u0020(saveCheckBox.isSelected())\u0020{\u0020saveChapterToFile(novelName,\u0020chapterTitle,\u0020chapterContent);}String\u0020cleanContent\u0020=\u0020cleanChapterContent(chapterContent);chapters.put(chapterTitle,\u0020cleanContent);updateChapterList();try\u0020{\u0020wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='Readpage\u0020pagedown']/a[@id='pb_next']")));WebElement\u0020nextButton\u0020=\u0020driver.findElement(By.xpath("//div[@class='Readpage\u0020pagedown']/a[@id='pb_next']"));nextButton.click();}\u0020catch\u0020(Exception\u0020e)\u0020{\u0020break;}\u0020}\u0020driver.navigate().back();}\u0020}\u0020}catch\u0020(Exception\u0020e)\u0020{\u0020e.printStackTrace();showAlert("提示",\u0020"运行出错",\u0020Alert.AlertType.ERROR);}finally\u0020{\u0020cleanupChromeDriver();}\u0020return\u0020null;}};Thread\u0020thread\u0020=\u0020new\u0020Thread(task);thread.setDaemon(true);thread.start();}\u0020private\u0020void\u0020setupChromeDriver()\u0020{\u0020ChromeOptions\u0020options\u0020=\u0020new\u0020ChromeOptions();options.addArguments("--no-sandbox");options.addArguments("--disable-dev-shm-usage");options.addArguments("--window-size=1920,1080");options.addArguments("user-agent='Mozilla/5.0\u0020(Windows\u0020NT\u002010.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'");driver\u0020=\u0020new\u0020ChromeDriver(options);}\u0020private\u0020void\u0020cleanupChromeDriver()\u0020{\u0020if\u0020(driver\u0020!=\u0020null)\u0020{\u0020driver.close();driver.quit();}\u0020}\u0020private\u0020void\u0020saveChapterToFile(String\u0020novelName,\u0020String\u0020chapterTitle,\u0020String\u0020chapterContent)\u0020{\u0020String\u0020bookDir\u0020=\u0020System.getProperty("user.dir")\u0020+\u0020"/"\u0020+\u0020novelName;File\u0020bookDirectory\u0020=\u0020new\u0020File(bookDir);if\u0020(!bookDirectory.exists())\u0020{\u0020bookDirectory.mkdirs();}\u0020String\u0020chapterPath\u0020=\u0020bookDir\u0020+\u0020"/"\u0020+\u0020chapterTitle\u0020+\u0020".txt";try\u0020(BufferedWriter\u0020writer\u0020=\u0020new\u0020BufferedWriter(new\u0020FileWriter(chapterPath)))\u0020{\u0020writer.write(chapterContent);}\u0020catch\u0020(IOException\u0020e)\u0020{\u0020e.printStackTrace();}\u0020}\u0020private\u0020String\u0020cleanChapterContent(String\u0020chapterContent)\u0020{\u0020Document\u0020doc\u0020=\u0020Jsoup.parse(chapterContent);Elements\u0020chapterBrElements\u0020=\u0020doc.select("br");return\u0020chapterBrElements.stream().map(brElement\u0020->\u0020brElement.nextSibling().toString().trim()).filter(text\u0020->\u0020!text.isEmpty()).collect(Collectors.joining("\n"));}\u0020public\u0020void\u0020updateChapterList()\u0020{\u0020Platform.runLater(()\u0020->\u0020{\u0020chapterList.getItems().clear();chapterList.getItems().addAll(chapters.keySet());});}\u0020public\u0020void\u0020showChapterContent(String\u0020chapterTitle)\u0020{\u0020Platform.runLater(()\u0020->\u0020{\u0020String\u0020content\u0020=\u0020chapters.get(chapterTitle);if\u0020(content\u0020!=\u0020null)\u0020{\u0020content\u0020=\u0020content.replaceAll("
",\u0020"\n\u0020"); // Replace
with spaceschapterText.setText(content);}\u0020});}\u0020public\u0020static\u0020void\u0020showAlert(String\u0020title,\u0020String\u0020message,\u0020Alert.AlertType\u0020alertType)\u0020{\u0020Platform.runLater(()\u0020->\u0020{\u0020Alert\u0020alert\u0020=\u0020new\u0020Alert(alertType);alert.setTitle(title);alert.setHeaderText(null);alert.setContentText(message);alert.showAndWait();});}\u0020@Override\u0020public\u0020void\u0020stop()\u0020{\u0020cleanupChromeDriver();}\u0020

小说下载器 - 免费在线下载小说

原文地址: https://www.cveoy.top/t/topic/nJZj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录