Skip to content

Instantly share code, notes, and snippets.

@dubiao
Created March 11, 2026 09:40
Show Gist options
  • Select an option

  • Save dubiao/4f4a625b58fb08d8ec7380eec60019ce to your computer and use it in GitHub Desktop.

Select an option

Save dubiao/4f4a625b58fb08d8ec7380eec60019ce to your computer and use it in GitHub Desktop.
获取苹果官翻机价格 Get Refurbished Macbook Prices
const https = require('https');
const url = 'https://www.apple.com.cn/shop/refurbished/mac/macbook-air-macbook-pro';
/**
* 字符平衡计数法提取对象字符串
*/
function extractObject(html, marker) {
const startIndex = html.indexOf(marker);
if (startIndex === -1) return null;
const jsonStartIndex = html.indexOf('{', startIndex);
if (jsonStartIndex === -1) return null;
let count = 0;
for (let i = jsonStartIndex; i < html.length; i++) {
if (html[i] === '{') {
count++;
} else if (html[i] === '}') {
count--;
if (count === 0) {
return html.substring(jsonStartIndex, i + 1);
}
}
}
return null;
}
https
.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
// 1. 提取目标字符串
const jsonStr = extractObject(data, 'window.REFURB_GRID_BOOTSTRAP');
if (!jsonStr) {
console.error('未找到 window.REFURB_GRID_BOOTSTRAP 数据');
return;
}
try {
// 2. 将字符串转换为 JS 对象
// 使用 new Function 避免直接 eval 的作用域污染,同时兼容非标准 JSON 格式
const bootstrapData = new Function(`return ${jsonStr}`)();
// 模拟 window 环境执行用户逻辑
const window = {REFURB_GRID_BOOTSTRAP: bootstrapData};
// 3. 执行过滤逻辑
const list = window.REFURB_GRID_BOOTSTRAP.tiles
.filter((t) => t.filters.dimensions.refurbClearModel.startsWith('macbook'))
.filter(
(t) => t.filters.dimensions.dimensionRelYear === '2025' || t.filters.dimensions.dimensionRelYear === '2024'
)
.filter((t) => !t.title.includes('Pro 芯片') && !t.title.includes('Max 芯片'))
.filter((t) => t.title.includes('M4') || t.title.includes('M5'))
.filter((t) => parseInt(t.filters.dimensions.tsMemorySize) > 16)
.filter((t) => parseInt(t.filters.dimensions.dimensionCapacity) > 256)
.map((t) => {
let chip1 = t.title.indexOf(' 芯片');
let chip0 = chip1 > 0 ? t.title.lastIndexOf(' ', chip1 - 1) : -1;
let chip = chip0 > 0 ? t.title.substring(chip0 + 1, chip1) : '--';
if (chip === 'Pro' || chip === 'Max') {
chip0 = t.title.lastIndexOf(' ', chip0 - 1);
chip = chip0 > 0 ? t.title.substring(chip0 + 1, chip1) : '--';
}
let screen = t.filters.dimensions.dimensionScreensize.replace('inch', '英寸');
if (t.title.includes('纳米纹理显示屏')) {
screen += ' + 纳米纹理显示屏';
}
return {
Model:
{
macbookair: 'Macbook Air',
macbookpro: 'Macbook Pro',
macbook: 'Macbook',
}[t.filters.dimensions.refurbClearModel] || t.filters.dimensions.refurbClearModel,
Chip: chip,
Memory: t.filters.dimensions.tsMemorySize.toUpperCase(),
Storage: t.filters.dimensions.dimensionCapacity.toUpperCase(),
Screen: screen,
Year: t.filters.dimensions.dimensionRelYear,
Price: parseInt(t.price.currentPrice.raw_amount),
// Link: 'https://www.apple.com.cn' + t.productDetailsUrl,
};
});
console.table(list);
} catch (e) {
console.error('解析或逻辑执行失败:', e.message);
}
});
})
.on('error', (e) => {
console.error('请求失败:', e.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment