92 lines
2.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 截图脚本 - 使用 Playwright
*/
const { chromium } = require('playwright');
const fs = require('fs');
const path = require('path');
const BASE_DIR = path.resolve(process.env.BASE_DIR || process.cwd());
const PIC_DIR = path.join(BASE_DIR, 'pic');
const TARGET_URL = 'http://bjjs.zjw.beijing.gov.cn/eportal/ui?pageId=307749';
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
// 口径调整:当天执行时,落地图按前一天日期命名
function getDataDate() {
const date = new Date();
date.setDate(date.getDate() - 1);
return formatDate(date);
}
async function screenshot(url, outputPath) {
console.log(`正在截图: ${url}`);
console.log(`输出路径: ${outputPath}`);
const browser = await chromium.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
try {
const context = await browser.newContext({
viewport: { width: 1920, height: 1080 }
});
const page = await context.newPage();
// 设置超时
page.setDefaultTimeout(60000);
page.setDefaultNavigationTimeout(60000);
// 访问页面
await page.goto(url, {
waitUntil: 'networkidle',
timeout: 60000
});
// 等待页面加载完成
await page.waitForLoadState('domcontentloaded');
// 额外等待3秒确保动态内容加载
await page.waitForTimeout(3000);
// 执行JS将所有 table 标签的 display 改为 table
await page.evaluate(() => {
const tables = document.querySelectorAll('table');
tables.forEach(table => {
table.style.display = 'table';
});
});
// 确保输出目录存在
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// 截图
await page.screenshot({
path: outputPath,
fullPage: true
});
console.log(` ✓ 截图已保存: ${outputPath}`);
} catch (error) {
console.error(` ✗ 截图失败: ${error.message}`);
process.exit(1);
} finally {
await browser.close();
}
}
// 主程序
screenshot(TARGET_URL, path.join(PIC_DIR, `${getDataDate()}.png`));