2026-01-11 23:32:09 +08:00
|
|
|
|
import * as puppeteer from 'puppeteer';
|
|
|
|
|
|
import { Logger } from '@nestjs/common';
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟人类鼠标移动
|
|
|
|
|
|
async function simulateHumanMouseMovement(page: puppeteer.Page) {
|
|
|
|
|
|
const viewport = page.viewport();
|
|
|
|
|
|
if (!viewport) return;
|
|
|
|
|
|
|
|
|
|
|
|
const movements = 5 + Math.floor(Math.random() * 5); // 5-10次随机移动
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < movements; i++) {
|
|
|
|
|
|
const x = Math.floor(Math.random() * viewport.width);
|
|
|
|
|
|
const y = Math.floor(Math.random() * viewport.height);
|
2026-01-14 22:26:32 +08:00
|
|
|
|
|
2026-01-11 23:32:09 +08:00
|
|
|
|
await page.mouse.move(x, y, {
|
2026-01-14 22:26:32 +08:00
|
|
|
|
steps: 10 + Math.floor(Math.random() * 20), // 10-30步,使移动更平滑
|
2026-01-11 23:32:09 +08:00
|
|
|
|
});
|
2026-01-14 22:26:32 +08:00
|
|
|
|
|
2026-01-11 23:32:09 +08:00
|
|
|
|
// 随机停顿 100-500ms
|
2026-01-14 22:26:32 +08:00
|
|
|
|
await new Promise((r) => setTimeout(r, 100 + Math.random() * 400));
|
2026-01-11 23:32:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟人类滚动
|
|
|
|
|
|
async function simulateHumanScrolling(page: puppeteer.Page) {
|
|
|
|
|
|
const scrollCount = 3 + Math.floor(Math.random() * 5); // 3-7次滚动
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < scrollCount; i++) {
|
|
|
|
|
|
const scrollDistance = 100 + Math.floor(Math.random() * 400); // 100-500px
|
2026-01-14 22:26:32 +08:00
|
|
|
|
|
2026-01-11 23:32:09 +08:00
|
|
|
|
await page.evaluate((distance) => {
|
|
|
|
|
|
window.scrollBy({
|
|
|
|
|
|
top: distance,
|
2026-01-14 22:26:32 +08:00
|
|
|
|
behavior: 'smooth',
|
2026-01-11 23:32:09 +08:00
|
|
|
|
});
|
|
|
|
|
|
}, scrollDistance);
|
|
|
|
|
|
|
|
|
|
|
|
// 随机停顿 500-1500ms
|
2026-01-14 22:26:32 +08:00
|
|
|
|
await new Promise((r) => setTimeout(r, 500 + Math.random() * 1000));
|
2026-01-11 23:32:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 滚动回顶部
|
|
|
|
|
|
await page.evaluate(() => {
|
|
|
|
|
|
window.scrollTo({ top: 0, behavior: 'smooth' });
|
|
|
|
|
|
});
|
2026-01-14 22:26:32 +08:00
|
|
|
|
await new Promise((r) => setTimeout(r, 1000));
|
2026-01-11 23:32:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-15 14:03:46 +08:00
|
|
|
|
// 检查错误是否为代理隧道连接失败
|
|
|
|
|
|
function isTunnelConnectionFailedError(error: unknown): boolean {
|
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
error.message.includes('net::ERR_TUNNEL_CONNECTION_FAILED') ||
|
|
|
|
|
|
error.message.includes('ERR_TUNNEL_CONNECTION_FAILED')
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 延迟重试函数
|
|
|
|
|
|
async function delayRetry(
|
|
|
|
|
|
operation: () => Promise<void>,
|
|
|
|
|
|
maxRetries: number = 3,
|
|
|
|
|
|
delayMs: number = 5000,
|
|
|
|
|
|
logger?: Logger,
|
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
|
let lastError: Error | unknown;
|
|
|
|
|
|
|
|
|
|
|
|
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await operation();
|
|
|
|
|
|
return;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
lastError = error;
|
|
|
|
|
|
|
|
|
|
|
|
if (isTunnelConnectionFailedError(error)) {
|
|
|
|
|
|
if (attempt < maxRetries) {
|
|
|
|
|
|
const delay = delayMs * attempt; // 递增延迟
|
|
|
|
|
|
logger?.warn(
|
|
|
|
|
|
`代理隧道连接失败,第 ${attempt} 次尝试失败,${delay / 1000} 秒后重试...`,
|
|
|
|
|
|
);
|
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
logger?.error(
|
|
|
|
|
|
`代理隧道连接失败,已达到最大重试次数 ${maxRetries} 次`,
|
|
|
|
|
|
);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 非代理错误,直接抛出
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw lastError;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-11 23:32:09 +08:00
|
|
|
|
export interface CnncecpResult {
|
|
|
|
|
|
title: string;
|
|
|
|
|
|
publishDate: Date;
|
|
|
|
|
|
url: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-14 22:26:32 +08:00
|
|
|
|
interface CnncecpCrawlerType {
|
|
|
|
|
|
name: string;
|
|
|
|
|
|
url: string;
|
|
|
|
|
|
baseUrl: string;
|
|
|
|
|
|
extract(html: string): CnncecpResult[];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-11 23:32:09 +08:00
|
|
|
|
export const CnncecpCrawler = {
|
|
|
|
|
|
name: '中核集团电子采购平台',
|
|
|
|
|
|
url: 'https://www.cnncecp.com/xzbgg/index.jhtml',
|
2026-01-12 18:59:17 +08:00
|
|
|
|
baseUrl: 'https://www.cnncecp.com/',
|
2026-01-11 23:32:09 +08:00
|
|
|
|
|
2026-01-14 22:26:32 +08:00
|
|
|
|
async crawl(
|
|
|
|
|
|
this: CnncecpCrawlerType,
|
|
|
|
|
|
browser: puppeteer.Browser,
|
|
|
|
|
|
): Promise<CnncecpResult[]> {
|
2026-01-11 23:32:09 +08:00
|
|
|
|
const logger = new Logger('CnncecpCrawler');
|
|
|
|
|
|
const page = await browser.newPage();
|
|
|
|
|
|
|
|
|
|
|
|
const username = process.env.PROXY_USERNAME;
|
|
|
|
|
|
const password = process.env.PROXY_PASSWORD;
|
|
|
|
|
|
if (username && password) {
|
|
|
|
|
|
await page.authenticate({ username, password });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await page.evaluateOnNewDocument(() => {
|
|
|
|
|
|
Object.defineProperty(navigator, 'webdriver', { get: () => false });
|
2026-01-14 22:26:32 +08:00
|
|
|
|
Object.defineProperty(navigator, 'language', { get: () => 'zh-CN' });
|
|
|
|
|
|
Object.defineProperty(navigator, 'plugins', {
|
|
|
|
|
|
get: () => [1, 2, 3, 4, 5],
|
|
|
|
|
|
});
|
2026-01-11 23:32:09 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-14 22:26:32 +08:00
|
|
|
|
await page.setUserAgent(
|
|
|
|
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36',
|
|
|
|
|
|
);
|
2026-01-11 23:32:09 +08:00
|
|
|
|
await page.setViewport({ width: 1920, height: 1080 });
|
|
|
|
|
|
|
|
|
|
|
|
const allResults: CnncecpResult[] = [];
|
|
|
|
|
|
let currentPage = 1;
|
|
|
|
|
|
const maxPages = 5;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
logger.log(`Navigating to ${this.url}...`);
|
2026-01-15 14:03:46 +08:00
|
|
|
|
await delayRetry(
|
|
|
|
|
|
async () => {
|
|
|
|
|
|
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
|
|
|
|
|
|
},
|
|
|
|
|
|
3,
|
|
|
|
|
|
5000,
|
|
|
|
|
|
logger,
|
|
|
|
|
|
);
|
2026-01-11 23:32:09 +08:00
|
|
|
|
|
|
|
|
|
|
// 模拟人类行为
|
|
|
|
|
|
logger.log('Simulating human mouse movements...');
|
|
|
|
|
|
await simulateHumanMouseMovement(page);
|
2026-01-14 22:26:32 +08:00
|
|
|
|
|
2026-01-11 23:32:09 +08:00
|
|
|
|
logger.log('Simulating human scrolling...');
|
|
|
|
|
|
await simulateHumanScrolling(page);
|
|
|
|
|
|
|
|
|
|
|
|
while (currentPage <= maxPages) {
|
|
|
|
|
|
logger.log(`Processing page ${currentPage}...`);
|
|
|
|
|
|
|
|
|
|
|
|
const content = await page.content();
|
|
|
|
|
|
const pageResults = this.extract(content);
|
|
|
|
|
|
|
|
|
|
|
|
if (pageResults.length === 0) {
|
|
|
|
|
|
logger.warn(`No results found on page ${currentPage}, stopping.`);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
allResults.push(...pageResults);
|
2026-01-14 22:26:32 +08:00
|
|
|
|
logger.log(
|
|
|
|
|
|
`Extracted ${pageResults.length} items from page ${currentPage}`,
|
|
|
|
|
|
);
|
2026-01-11 23:32:09 +08:00
|
|
|
|
|
|
|
|
|
|
// 模拟人类行为 - 翻页前
|
|
|
|
|
|
logger.log('Simulating human mouse movements before pagination...');
|
|
|
|
|
|
await simulateHumanMouseMovement(page);
|
2026-01-14 22:26:32 +08:00
|
|
|
|
|
2026-01-11 23:32:09 +08:00
|
|
|
|
logger.log('Simulating human scrolling before pagination...');
|
|
|
|
|
|
await simulateHumanScrolling(page);
|
|
|
|
|
|
|
|
|
|
|
|
// 查找下一页按钮
|
|
|
|
|
|
const nextButtonSelector = 'a[href*="index_"]';
|
|
|
|
|
|
const nextButton = await page.$(nextButtonSelector);
|
|
|
|
|
|
|
|
|
|
|
|
if (!nextButton) {
|
|
|
|
|
|
logger.log('Next page button not found. Reached end of list.');
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.log(`Navigating to page ${currentPage + 1}...`);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 点击下一页按钮
|
|
|
|
|
|
await nextButton.click();
|
2026-01-14 22:26:32 +08:00
|
|
|
|
await new Promise((r) => setTimeout(r, 3000)); // 等待页面加载
|
2026-01-11 23:32:09 +08:00
|
|
|
|
} catch (navError) {
|
2026-01-14 22:26:32 +08:00
|
|
|
|
const navErrorMessage =
|
|
|
|
|
|
navError instanceof Error ? navError.message : String(navError);
|
|
|
|
|
|
logger.error(
|
|
|
|
|
|
`Navigation to page ${currentPage + 1} failed: ${navErrorMessage}`,
|
|
|
|
|
|
);
|
2026-01-11 23:32:09 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
currentPage++;
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟人类行为 - 翻页后
|
|
|
|
|
|
logger.log('Simulating human mouse movements after pagination...');
|
|
|
|
|
|
await simulateHumanMouseMovement(page);
|
2026-01-14 22:26:32 +08:00
|
|
|
|
|
2026-01-11 23:32:09 +08:00
|
|
|
|
logger.log('Simulating human scrolling after pagination...');
|
|
|
|
|
|
await simulateHumanScrolling(page);
|
|
|
|
|
|
|
|
|
|
|
|
// Random delay between pages
|
|
|
|
|
|
const delay = Math.floor(Math.random() * (3000 - 1000 + 1)) + 1000;
|
2026-01-14 22:26:32 +08:00
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
2026-01-11 23:32:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return allResults;
|
|
|
|
|
|
} catch (error) {
|
2026-01-14 22:26:32 +08:00
|
|
|
|
const errorMessage =
|
|
|
|
|
|
error instanceof Error ? error.message : String(error);
|
|
|
|
|
|
logger.error(`Failed to crawl ${this.name}: ${errorMessage}`);
|
2026-01-15 14:28:04 +08:00
|
|
|
|
throw error;
|
2026-01-11 23:32:09 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
await page.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
2026-01-14 22:26:32 +08:00
|
|
|
|
extract(this: CnncecpCrawlerType, html: string): CnncecpResult[] {
|
2026-01-11 23:32:09 +08:00
|
|
|
|
const results: CnncecpResult[] = [];
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Regex groups for cnncecp.com:
|
|
|
|
|
|
* 1: Date (发布时间,格式:2026-01-11)
|
|
|
|
|
|
* 2: URL (href属性)
|
|
|
|
|
|
* 3: Title (a标签文本)
|
|
|
|
|
|
*
|
|
|
|
|
|
* HTML结构示例:
|
|
|
|
|
|
* <li>
|
|
|
|
|
|
* <span class="Right Gray">2026-01-11</span>
|
|
|
|
|
|
* <span class="Right Right20"><em class="Red">文件下载截止:2025-12-08 23:59:00</em></span>
|
|
|
|
|
|
* <span class="Blue">[变更公告]</span>
|
|
|
|
|
|
* <a href="https://www.cnncecp.com/xzbgg/1862778.jhtml">中核四0四有限公司2026-2028年度质量流量控制器等采购项目(二次)变更公告</a>
|
|
|
|
|
|
* </li>
|
|
|
|
|
|
*/
|
2026-01-14 22:26:32 +08:00
|
|
|
|
const regex =
|
|
|
|
|
|
/<li>[\s\S]*?<span class="Right Gray">\s*(\d{4}-\d{2}-\d{2})\s*<\/span>[\s\S]*?<a[^>]*href="([^"]*)"[^>]*>([^<]*)<\/a>[\s\S]*?<\/li>/gs;
|
2026-01-11 23:32:09 +08:00
|
|
|
|
|
2026-01-14 22:26:32 +08:00
|
|
|
|
let match: RegExpExecArray | null;
|
2026-01-11 23:32:09 +08:00
|
|
|
|
while ((match = regex.exec(html)) !== null) {
|
2026-01-14 22:26:32 +08:00
|
|
|
|
const dateStr = match[1]?.trim() ?? '';
|
|
|
|
|
|
const url = match[2]?.trim() ?? '';
|
|
|
|
|
|
const title = match[3]?.trim() ?? '';
|
2026-01-11 23:32:09 +08:00
|
|
|
|
|
|
|
|
|
|
if (title && url) {
|
2026-01-13 18:07:00 +08:00
|
|
|
|
const fullUrl = url.startsWith('http') ? url : this.baseUrl + url;
|
2026-01-11 23:32:09 +08:00
|
|
|
|
results.push({
|
|
|
|
|
|
title,
|
|
|
|
|
|
publishDate: dateStr ? new Date(dateStr) : new Date(),
|
2026-01-14 22:26:32 +08:00
|
|
|
|
url: fullUrl.replace(/\/\//g, '/'),
|
2026-01-11 23:32:09 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return results;
|
2026-01-14 22:26:32 +08:00
|
|
|
|
},
|
2026-01-11 23:32:09 +08:00
|
|
|
|
};
|