search.js
1 import { AuthRequiredError } from '@jackwener/opencli/errors'; 2 import { cli, Strategy } from '@jackwener/opencli/registry'; 3 import { clampInt, requireNonEmptyQuery } from '../_shared/common.js'; 4 cli({ 5 site: 'taobao', 6 name: 'search', 7 description: '淘宝商品搜索', 8 domain: 's.taobao.com', 9 strategy: Strategy.COOKIE, 10 args: [ 11 { name: 'query', positional: true, required: true, help: '搜索关键词' }, 12 { name: 'sort', default: 'default', choices: ['default', 'sale', 'price'], help: '排序 (default/sale销量/price价格)' }, 13 { name: 'limit', type: 'int', default: 10, help: '返回结果数量 (max 40)' }, 14 ], 15 columns: ['rank', 'title', 'price', 'sales', 'shop', 'location', 'item_id', 'url'], 16 navigateBefore: false, 17 func: async (page, kwargs) => { 18 const limit = clampInt(kwargs.limit, 10, 1, 40); 19 const query = requireNonEmptyQuery(kwargs.query); 20 const sortMap = { default: '', sale: '&sort=sale-desc', price: '&sort=price-asc' }; 21 const sortParam = sortMap[String(kwargs.sort || 'default')] || ''; 22 await page.goto('https://www.taobao.com'); 23 await page.wait(2); 24 await page.evaluate(`location.href = ${JSON.stringify(`https://s.taobao.com/search?q=${encodeURIComponent(query)}${sortParam}`)}`); 25 await page.wait(8); 26 await page.autoScroll({ times: 3, delayMs: 2000 }); 27 const data = await page.evaluate(` 28 (async () => { 29 const normalize = v => (v || '').replace(/\\s+/g, ' ').trim(); 30 const bodyText = document.body?.innerText || ''; 31 if (bodyText.length < 1000 && bodyText.includes('请登录')) { 32 return { error: 'auth-required' }; 33 } 34 35 for (let i = 0; i < 30; i++) { 36 if (document.querySelectorAll('[class*="doubleCard--"]').length > 3) break; 37 await new Promise(r => setTimeout(r, 500)); 38 } 39 40 const cards = document.querySelectorAll('[class*="doubleCard--"]'); 41 const results = []; 42 const seenTitles = new Set(); 43 44 for (const card of cards) { 45 const titleEl = card.querySelector('[class*="title--"]'); 46 const title = titleEl ? normalize(titleEl.textContent) : ''; 47 if (!title || title.length < 3 || seenTitles.has(title)) continue; 48 seenTitles.add(title); 49 50 const intEl = card.querySelector('[class*="priceInt--"]'); 51 const floatEl = card.querySelector('[class*="priceFloat--"]'); 52 let price = ''; 53 if (intEl) { 54 price = '¥' + normalize(intEl.textContent) + (floatEl ? normalize(floatEl.textContent) : ''); 55 } 56 57 const salesEl = card.querySelector('[class*="realSales--"]'); 58 const sales = salesEl ? normalize(salesEl.textContent) : ''; 59 60 const shopEl = card.querySelector('[class*="shopName--"]'); 61 let shop = shopEl ? normalize(shopEl.textContent) : ''; 62 shop = shop.replace(/^\\d+年老店/, '').replace(/^回头客[\\d万]+/, ''); 63 64 const locEls = card.querySelectorAll('[class*="procity--"]'); 65 const location = Array.from(locEls).map(el => normalize(el.textContent)).join(''); 66 67 let itemId = ''; 68 let wrapper = card.parentElement; 69 for (let i = 0; i < 3 && wrapper; i++) { 70 const spmId = wrapper.getAttribute('data-spm-act-id'); 71 if (spmId && /^\\d{10,}$/.test(spmId)) { itemId = spmId; break; } 72 wrapper = wrapper.parentElement; 73 } 74 75 results.push({ 76 rank: results.length + 1, 77 title: title.slice(0, 80), 78 price, 79 sales, 80 shop, 81 location, 82 item_id: itemId, 83 url: itemId ? 'https://item.taobao.com/item.htm?id=' + itemId : '', 84 }); 85 if (results.length >= ${limit}) break; 86 } 87 88 return { results }; 89 })() 90 `); 91 if (data?.error === 'auth-required') { 92 throw new AuthRequiredError('taobao search requires a logged-in Taobao session'); 93 } 94 return Array.isArray(data?.results) ? data.results : []; 95 }, 96 });