前言
需要一直关注网站有没有出成绩结果,一直盯着好麻烦的说
既然是程序员,应该可以做个定时任务代劳吧
代码
使用的是selenium
SpringBoot环境,JDK17
这只是一个简陋版,做出来不需要太多代码
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 依赖可以在父类里找到 -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-edge-driver</artifactId>
</dependency>
Java
private static final AtomicBoolean OK_RESULT = new AtomicBoolean(false);
@Scheduled(cron = "0/30 * * * * ? ")
pubilc void watchHtml(){
if (OK_RESULT.get()){
log.info("快去看看,已经出现变化啦!!!!!!");
}
String url = "https://www.baidu.com";
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.addArguments("--headless"); // 启动无头模式
edgeOptions.addArguments("--disable-gpu"); // 禁用 GPU(通常在无头模式下启用)
WebDriver driver = new EdgeDriver(edgeOptions);
try {
driver.get(url);
//睡一下,以让网页执行完毕JS
Thread.sleep(3000);
String pageSource = driver.getPageSource();
log.info(pageSource);
// JavascriptExecutor js = (JavascriptExecutor) driver;
// String finalHtml = (String) js.executeScript("return document.documentElement.outerHTML");
// log.info(finalHtml);
if (!pageSource.contains("当前无正在进行的考试阶段")){
log.info("出现变化!!有内容了!!!");
OK_RESULT.set(true);
}
}catch (Exception e){
throw new RuntimeException(e);
}finally {
driver.quit();
}
}
标签:info,网页,log,selenium,driver,访问,new,Java,edgeOptions
From: https://www.cnblogs.com/lazykingloveu/p/18680815