minm-api/server.js
2025-05-23 10:36:26 +08:00

69 lines
2.3 KiB
JavaScript
Raw 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.

const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const cors = require('cors');
const app = express();
const PORT = 3213;
// 引入并使用cors中间件允许所有跨域访问
app.use(cors());
app.get('/api/minm/open', async (req, res) => {
try {
const { data } = await axios.get('https://stockanalysis.com/quote/otc/MINM/');
const $ = cheerio.load(data);
let openValue = null;
let volumeValue = null;
let daysRangeValue = null;
let week52RangeValue = null;
let marketCapValue = null;
let changeValue = [];
const priceDiv = $("div.text-4xl.font-bold.transition-colors.duration-300.inline-block").first();
const price = priceDiv.text().trim();
const changeDiv = priceDiv.next("div.font-semibold.inline-block.text-2xl.text-red-vivid");
const change = changeDiv.text().trim();
if (price && change) {
changeValue = [price, change];
}
$('td').each((i, el) => {
if ($(el).text().trim() === 'Open') {
openValue = $(el).next('td').text().trim();
}
if ($(el).text().trim() === 'Volume') {
volumeValue = $(el).next('td').text().trim();
}
if ($(el).text().trim() === "Day's Range") {
daysRangeValue = $(el).next('td').text().trim();
}
if ($(el).text().trim() === '52-Week Range') {
week52RangeValue = $(el).next('td').text().trim();
}
if ($(el).text().trim() === 'Market Cap') {
marketCapValue = $(el).next('td').text().trim();
}
});
if (openValue && volumeValue && daysRangeValue && week52RangeValue && marketCapValue && changeValue.length === 2) {
res.json({ Open: openValue, Volume: volumeValue, DaysRange: daysRangeValue, Week52Range: week52RangeValue, MarketCap: marketCapValue, change: changeValue });
} else {
res.status(404).json({
error: '未找到全部数据',
Open: openValue,
Volume: volumeValue,
DaysRange: daysRangeValue,
Week52Range: week52RangeValue,
MarketCap: marketCapValue,
change: changeValue
});
}
} catch (err) {
res.status(500).json({ error: '请求或解析失败', detail: err.message });
}
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});