为什么会报错
错误: 找不到或无法加载主类 crawier.Crawier
原因: java.lang.ClassNotFoundException: crawier.Crawier

package crawier;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Crawier {
public static void main(String[] args) {
// 设置 WebDriver 的路径
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
String url = "https://www.shanghairanking.cn/rankings/bcur/2024";
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(url);
List<Map<String, String>> ulist = new ArrayList<>();
int page = 1;
while (page <= 20) {
// 等待页面加载完成
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("tbody")));
Document doc = Jsoup.parse(driver.getPageSource());
ulist.addAll(extractUniversityInfo(doc));
scrollPage(driver);
nextPage(driver, "li[title='下一页']");
page++;
}
// 保存为JSON文件
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writerWithDefaultPrettyPrinter().writeValue(new File("university_rankings.json"), ulist);
} catch (IOException e) {
e.printStackTrace();
}
driver.quit();
}
private static List<Map<String, String>> extractUniversityInfo(Document doc) {
Elements trs = doc.select("tbody tr");
List<Map<String, String>> ulist = new ArrayList<>();
for (Element tr : trs) {
if (tr.tagName().equals("tr")) {
Elements tds = tr.select("td");
Map<String, String> university = extractUniversityDetails(tds);
ulist.add(university);
}
}
return ulist;
}
private static Map<String, String> extractUniversityDetails(Elements tds) {
Map<String, String> university = Map.of(
"排名", tds.get(0).select("div.ranking").text(),
"logo", tds.get(1).select("img").attr("src"),
"中文名", tds.get(1).select("a").get(0).text(),
"英文名", tds.get(1).select("a").get(1).text(),
"标签", tds.get(1).select("p").text(),
"地址", tds.get(2).text(),
"行业", tds.get(3).text(),
"分数", tds.get(4).text(),
"层次", tds.get(5).text()
);
return university;
}
private static void scrollPage(WebDriver driver) {
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
}
private static void nextPage(WebDriver driver, String cssSelector) {
new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(cssSelector)))
.click();
try {
Thread.sleep(1000); // 可以替换为更合适的等待方式,如 WebDriverWait
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}