detail.js
1 import { cli, Strategy } from '@jackwener/opencli/registry'; 2 import { normalizeNumericId } from '../_shared/common.js'; 3 cli({ 4 site: 'taobao', 5 name: 'detail', 6 description: '淘宝商品详情', 7 domain: 'item.taobao.com', 8 strategy: Strategy.COOKIE, 9 args: [ 10 { name: 'id', positional: true, required: true, help: '商品 ID' }, 11 ], 12 columns: ['field', 'value'], 13 navigateBefore: false, 14 func: async (page, kwargs) => { 15 const itemId = normalizeNumericId(kwargs.id, 'id', '827563850178'); 16 await page.goto('https://www.taobao.com'); 17 await page.wait(2); 18 await page.evaluate(`location.href = ${JSON.stringify(`https://item.taobao.com/item.htm?id=${itemId}`)}`); 19 await page.wait(6); 20 const data = await page.evaluate(` 21 (() => { 22 const normalize = v => (v || '').replace(/\\s+/g, ' ').trim(); 23 const text = document.body?.innerText || ''; 24 const results = []; 25 26 const titleEl = document.querySelector('[class*="mainTitle--"]'); 27 const title = titleEl ? normalize(titleEl.textContent) : document.title.split('-')[0].replace(/^【[^】]+】/, '').trim(); 28 results.push({ field: '商品名称', value: title.slice(0, 100) }); 29 30 const pricePattern = /[¥¥]\\s*(\\d+(?:\\.\\d{1,2})?)/g; 31 const prices = []; 32 let m; 33 while ((m = pricePattern.exec(text)) && prices.length < 3) { 34 const p = parseFloat(m[1]); 35 if (p > 0.1 && p < 100000) prices.push(p); 36 } 37 if (prices.length > 0) { 38 results.push({ field: '价格', value: '¥' + Math.min(...prices) }); 39 } 40 41 const salesMatch = text.match(/(\\d+万?\\+?)\\s*人付款/) || text.match(/月销\\s*(\\d+万?\\+?)/); 42 if (salesMatch) results.push({ field: '销量', value: salesMatch[0] }); 43 44 const reviewMatch = text.match(/累计评价\\s*(\\d+万?\\+?)/) || text.match(/评价[((]\\s*(\\d+万?\\+?)/); 45 if (reviewMatch) results.push({ field: '评价数', value: reviewMatch[1] }); 46 47 const ratingMatch = text.match(/(\\d+\\.\\d)\\s*(?:分|描述|物流|服务)/); 48 if (ratingMatch) results.push({ field: '店铺评分', value: ratingMatch[0] }); 49 50 const shopMatch = text.match(/([\u4e00-\u9fa5A-Za-z0-9]{2,15}(?:旗舰店|专卖店|企业店|专营店))/); 51 if (shopMatch) results.push({ field: '店铺', value: shopMatch[1] }); 52 53 const locMatch = text.match(/发货地[::]*\\s*([\u4e00-\u9fa5]{2,10})/) || text.match(/([\u4e00-\u9fa5]{2,4}(?:省|市))\\s*发货/); 54 if (locMatch) results.push({ field: '发货地', value: locMatch[1] }); 55 56 if (text.includes('颜色分类')) { 57 const start = text.indexOf('颜色分类'); 58 const specSection = start >= 0 ? text.substring(start, start + 200) : ''; 59 const specs = specSection.split('\\n').filter(l => l.trim().length > 2 && l.trim().length < 50).slice(0, 5); 60 if (specs.length) results.push({ field: '可选规格', value: specs.join(' | ') }); 61 } 62 63 results.push({ field: 'ID', value: ${JSON.stringify(itemId)} }); 64 results.push({ field: '链接', value: location.href.split('&')[0] }); 65 return results; 66 })() 67 `); 68 return Array.isArray(data) ? data : []; 69 }, 70 });