停止使用puppeteer,换用axios与cheerio。
This commit is contained in:
42
GoldPrice.js
42
GoldPrice.js
@@ -19,7 +19,8 @@
|
||||
// console.log('Response:', response);
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const puppeteer = require('puppeteer-core');
|
||||
const axios = require('axios');
|
||||
const cheerio = require('cheerio');
|
||||
|
||||
const TARGET_URL = 'https://mybank.icbc.com.cn/icbc/newperbank/perbank3/gold/goldaccrual_query_out.jsp';
|
||||
const PRICE_SELECTOR = '#activeprice_080020000521';
|
||||
@@ -134,24 +135,31 @@ function isInDisabledWindow(ts = Date.now()) {
|
||||
}
|
||||
|
||||
async function fetchCurrentPrice() {
|
||||
console.log('[gold] launching browser to fetch price...');
|
||||
const browser = await puppeteer.launch({
|
||||
headless: 'new',
|
||||
args: ['--no-sandbox', '--disable-setuid-sandbox'],
|
||||
executablePath: process.env.CHROME_PATH || '/usr/bin/chromium-browser' // 默认路径,可通过环境变量覆盖
|
||||
console.log('[gold] fetching html to parse price...');
|
||||
const resp = await axios.get(TARGET_URL, {
|
||||
timeout: 60000,
|
||||
headers: {
|
||||
// 部分站点会根据 UA 返回不同内容,给一个常见 UA 更稳
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Pragma': 'no-cache',
|
||||
},
|
||||
// 某些服务端会返回非 200 但仍有 HTML,这里保持默认即可;若后续遇到问题再放开 validateStatus
|
||||
});
|
||||
try {
|
||||
const page = await browser.newPage();
|
||||
await page.goto(TARGET_URL, { waitUntil: 'domcontentloaded', timeout: 60000 });
|
||||
await page.waitForSelector(PRICE_SELECTOR, { timeout: 60000 });
|
||||
const text = await page.$eval(PRICE_SELECTOR, el => el.textContent || el.innerText || '');
|
||||
const numeric = parseFloat(String(text).replace(/[^0-9.\-]/g, ''));
|
||||
if (!isFinite(numeric)) throw new Error('无法解析价格');
|
||||
console.log(`[gold] fetched current price: ${numeric}`);
|
||||
return numeric;
|
||||
} finally {
|
||||
await browser.close();
|
||||
|
||||
const html = resp && resp.data ? String(resp.data) : '';
|
||||
if (!html) throw new Error('目标页面返回空内容');
|
||||
|
||||
const $ = cheerio.load(html);
|
||||
const text = $(PRICE_SELECTOR).text().trim();
|
||||
const numeric = parseFloat(String(text).replace(/[^0-9.\-]/g, ''));
|
||||
if (!isFinite(numeric)) {
|
||||
throw new Error(`无法解析价格,selector=${PRICE_SELECTOR},text=${JSON.stringify(text)}`);
|
||||
}
|
||||
console.log(`[gold] fetched current price: ${numeric}`);
|
||||
return numeric;
|
||||
}
|
||||
|
||||
function findDeltaWithin(history, minutes) {
|
||||
|
||||
Reference in New Issue
Block a user