add -r
This commit is contained in:
parent
2518b922af
commit
b6f285bf68
@ -29,9 +29,8 @@ public class EtsScraper {
|
|||||||
private static final String USERNAME = "sccw";
|
private static final String USERNAME = "sccw";
|
||||||
private static final String PASSWORD = "slife@123";
|
private static final String PASSWORD = "slife@123";
|
||||||
private static final Path SCREENSHOT_DIR = Path.of("screenshots");
|
private static final Path SCREENSHOT_DIR = Path.of("screenshots");
|
||||||
private static final String OLLAMA_URL = "http://10.0.1.39:11434";
|
private static final String OLLAMA_URL = "http://127.0.0.1:11434";
|
||||||
private static final String OLLAMA_MODEL = "qwen3-vl:4b";
|
private static final String OLLAMA_MODEL = "qwen3-vl:4b";
|
||||||
private static final String PROXY_HOST = "http://127.0.0.1:8081";
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
// Parse CLI arguments
|
// Parse CLI arguments
|
||||||
@ -39,6 +38,7 @@ public class EtsScraper {
|
|||||||
String proxyUser = null;
|
String proxyUser = null;
|
||||||
String proxyPass = null;
|
String proxyPass = null;
|
||||||
String dateStr = null;
|
String dateStr = null;
|
||||||
|
Integer repeatInterval = null;
|
||||||
|
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
switch (args[i]) {
|
switch (args[i]) {
|
||||||
@ -57,6 +57,9 @@ public class EtsScraper {
|
|||||||
case "-d":
|
case "-d":
|
||||||
dateStr = args[++i];
|
dateStr = args[++i];
|
||||||
break;
|
break;
|
||||||
|
case "-r":
|
||||||
|
repeatInterval = Integer.parseInt(args[++i]);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
System.err.println("[-] Unknown option: " + args[i]);
|
System.err.println("[-] Unknown option: " + args[i]);
|
||||||
printHelp();
|
printHelp();
|
||||||
@ -77,13 +80,42 @@ public class EtsScraper {
|
|||||||
System.err.println("[-] Invalid date format: " + dateStr + ", expected yyyy-MM-dd");
|
System.err.println("[-] Invalid date format: " + dateStr + ", expected yyyy-MM-dd");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (repeatInterval != null) {
|
||||||
|
// Polling mode: run every N seconds
|
||||||
|
System.out.println("[*] Polling mode: running every " + repeatInterval + " seconds (target date: " + dateStr + ")");
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
runScraper(proxyHost, proxyUser, proxyPass, targetDate, true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("[-] Scraper failed: " + e.getMessage());
|
||||||
|
}
|
||||||
|
System.out.println("[*] Sleeping " + repeatInterval + "s before next run...");
|
||||||
|
sleep(repeatInterval * 1000L);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Single-shot mode: run once
|
||||||
|
runScraper(proxyHost, proxyUser, proxyPass, targetDate, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void runScraper(String proxyHost, String proxyUser, String proxyPass,
|
||||||
|
java.time.LocalDate targetDate, boolean isPolling) throws Exception {
|
||||||
String dateStrFormatted = targetDate.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
String dateStrFormatted = targetDate.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||||
|
|
||||||
String dateStrFileName = targetDate.format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmm"));
|
// In one-shot mode, use target date as filename (for "file exists" check)
|
||||||
|
// In polling mode, use current time as filename to always re-run
|
||||||
|
String dateStrFileName;
|
||||||
|
if (isPolling) {
|
||||||
|
dateStrFileName = java.time.LocalDateTime.now()
|
||||||
|
.format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmm"));
|
||||||
|
} else {
|
||||||
|
dateStrFileName = targetDate.format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmm"));
|
||||||
|
}
|
||||||
|
|
||||||
Path savedFile = Path.of("downloads").resolve("三联单列表_" + dateStrFileName + ".xls");
|
Path savedFile = Path.of("downloads").resolve("三联单列表_" + dateStrFileName + ".xls");
|
||||||
|
|
||||||
if (java.nio.file.Files.exists(savedFile) && java.nio.file.Files.size(savedFile) > 0) {
|
if (!isPolling && java.nio.file.Files.exists(savedFile) && java.nio.file.Files.size(savedFile) > 0) {
|
||||||
System.out.println("[+] File already exists: " + savedFile);
|
System.out.println("[+] File already exists: " + savedFile);
|
||||||
System.out.println("[+] File size: " + java.nio.file.Files.size(savedFile) + " bytes");
|
System.out.println("[+] File size: " + java.nio.file.Files.size(savedFile) + " bytes");
|
||||||
autoImportBill(savedFile, proxyHost, proxyUser, proxyPass);
|
autoImportBill(savedFile, proxyHost, proxyUser, proxyPass);
|
||||||
@ -96,6 +128,8 @@ public class EtsScraper {
|
|||||||
System.err.println("Failed to create directories: " + e.getMessage());
|
System.err.println("Failed to create directories: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println("[*] --- Scraper run at " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + " ---");
|
||||||
|
|
||||||
try (Playwright playwright = Playwright.create()) {
|
try (Playwright playwright = Playwright.create()) {
|
||||||
Browser browser = playwright.chromium().launch(
|
Browser browser = playwright.chromium().launch(
|
||||||
new BrowserType.LaunchOptions()
|
new BrowserType.LaunchOptions()
|
||||||
@ -128,8 +162,6 @@ public class EtsScraper {
|
|||||||
// Close notification dialog FIRST (before filling credentials)
|
// Close notification dialog FIRST (before filling credentials)
|
||||||
closeNotificationDialog(page);
|
closeNotificationDialog(page);
|
||||||
|
|
||||||
// screenshot(page, "after_close_dialog");
|
|
||||||
|
|
||||||
// Download captcha image
|
// Download captcha image
|
||||||
downloadCaptcha(page);
|
downloadCaptcha(page);
|
||||||
|
|
||||||
@ -143,8 +175,6 @@ public class EtsScraper {
|
|||||||
System.out.println("[+] Login successful!");
|
System.out.println("[+] Login successful!");
|
||||||
sleep(2000);
|
sleep(2000);
|
||||||
|
|
||||||
// screenshot(page, "after_login");
|
|
||||||
|
|
||||||
System.out.println("[+] Page title: " + page.title());
|
System.out.println("[+] Page title: " + page.title());
|
||||||
System.out.println("[+] Page URL: " + page.url());
|
System.out.println("[+] Page URL: " + page.url());
|
||||||
|
|
||||||
@ -152,8 +182,6 @@ public class EtsScraper {
|
|||||||
System.out.println("[*] Clicking 三联单 menu...");
|
System.out.println("[*] Clicking 三联单 menu...");
|
||||||
page.locator("#module_2094F683-C542-4904-B33E-0D227C4DE199").first().click();
|
page.locator("#module_2094F683-C542-4904-B33E-0D227C4DE199").first().click();
|
||||||
sleep(3000);
|
sleep(3000);
|
||||||
|
|
||||||
// screenshot(page, "after_sanliandan");
|
|
||||||
System.out.println("[+] 三联单 page title: " + page.title());
|
System.out.println("[+] 三联单 page title: " + page.title());
|
||||||
|
|
||||||
// 设置日期筛选
|
// 设置日期筛选
|
||||||
@ -186,7 +214,6 @@ public class EtsScraper {
|
|||||||
// 点击查询按钮,等待列表加载
|
// 点击查询按钮,等待列表加载
|
||||||
if (queryBtnExists) {
|
if (queryBtnExists) {
|
||||||
System.out.println("[*] Clicking query button...");
|
System.out.println("[*] Clicking query button...");
|
||||||
// 等待列表内容出现
|
|
||||||
page.waitForResponse("https://101.227.180.215/SHCityEnvCW/Services/CWSServ.asmx/ThreeBillQueryBiTripList", () -> {
|
page.waitForResponse("https://101.227.180.215/SHCityEnvCW/Services/CWSServ.asmx/ThreeBillQueryBiTripList", () -> {
|
||||||
page.locator("#Search_ThreeBillList_Button").first().click();
|
page.locator("#Search_ThreeBillList_Button").first().click();
|
||||||
});
|
});
|
||||||
@ -195,13 +222,9 @@ public class EtsScraper {
|
|||||||
System.out.println("[!] Query button not found");
|
System.out.println("[!] Query button not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
// screenshot(page, "after_query");
|
|
||||||
|
|
||||||
// 点击导出按钮
|
// 点击导出按钮
|
||||||
if (page.locator("#Export_ThreeBillList_Button").count() > 0) {
|
if (page.locator("#Export_ThreeBillList_Button").count() > 0) {
|
||||||
System.out.println("[*] Clicking export button...");
|
System.out.println("[*] Clicking export button...");
|
||||||
// 设置下载目录
|
|
||||||
// 点击主导出按钮打开对话框,再用 JS click 触发对话框内导出按钮
|
|
||||||
Download dl = page.waitForDownload(new Page.WaitForDownloadOptions().setTimeout(300000),
|
Download dl = page.waitForDownload(new Page.WaitForDownloadOptions().setTimeout(300000),
|
||||||
() -> {
|
() -> {
|
||||||
page.locator("#Export_ThreeBillList_Button").first().click();
|
page.locator("#Export_ThreeBillList_Button").first().click();
|
||||||
@ -220,11 +243,9 @@ public class EtsScraper {
|
|||||||
autoImportBill(savedFile, proxyHost, proxyUser, proxyPass);
|
autoImportBill(savedFile, proxyHost, proxyUser, proxyPass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// screenshot(page, "after_export");
|
|
||||||
System.out.println("[+] Query and export completed!");
|
System.out.println("[+] Query and export completed!");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("[-] Login failed. Check screenshots/ for debugging.");
|
System.out.println("[-] Login failed. Check screenshots/ for debugging.");
|
||||||
// screenshot(page, "login_failed");
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
browser.close();
|
browser.close();
|
||||||
@ -232,6 +253,7 @@ public class EtsScraper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void printHelp() {
|
private static void printHelp() {
|
||||||
System.out.println("""
|
System.out.println("""
|
||||||
ETS 三联单爬虫 - 导出并导入三联单 Excel 数据
|
ETS 三联单爬虫 - 导出并导入三联单 Excel 数据
|
||||||
@ -243,10 +265,12 @@ public class EtsScraper {
|
|||||||
-u <user> ets-proxy 用户名
|
-u <user> ets-proxy 用户名
|
||||||
-p <pass> ets-proxy 密码
|
-p <pass> ets-proxy 密码
|
||||||
-d <date> 查询日期,格式 yyyy-MM-dd
|
-d <date> 查询日期,格式 yyyy-MM-dd
|
||||||
|
-r <seconds> 定时执行间隔(秒),不传则只执行一次
|
||||||
-h 显示此帮助信息
|
-h 显示此帮助信息
|
||||||
|
|
||||||
示例:
|
示例:
|
||||||
java -jar ets-playwright.jar -s https://api.ets.niko.red -u admin -p 123456 -d 2026-05-04
|
java -jar ets-playwright.jar -s http://127.0.0.1:8081 -u sccw -p slife@123 -d 2026-05-21
|
||||||
|
java -jar ets-playwright.jar -s http://127.0.0.1:8081 -u sccw -p slife@123 -d 2026-05-21 -r 300
|
||||||
""");
|
""");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user