import axios from 'axios'; import * as cheerio from 'cheerio'; import fs from 'fs'; async function extractMultipleImages() { console.log('=== ADVANCED IMAGE EXTRACTOR ==='); const productUrls = [ { url: 'https://detail.1688.com/offer/951410798382.html?offerId=951410798382&sortType=&pageId=&abBizDataType=cbuOffer&trace_log=normal&uuid=07b8c17e56ce44729ee121592c608374&skuId=5869791617807&forcePC=1769593077326', name: '辣妹T恤' }, { url: 'https://detail.1688.com/offer/863185095565.html?offerId=863185095565&sortType=&pageId=&abBizDataType=cbuOffer&trace_log=normal&uuid=07b8c17e56ce44729ee121592c608374&skuId=5687254561570&forcePC=1769594105037', name: '白色长袖打底衫' } ]; const cookies = [ 'mtop_partitioned_detect=1', 't=d045a542d9d514096e017a885f5dcb91', 'sgcookie=E100lGD4JADOn7x3xLZ32JvX6bpd7zVZDZ5fWuweG9PMhr69fLkBddgOG6O4ct%2FVCBIDpovMys1Wqk1ypG0IqdjzlZjaLsO3oL2M60yXaunPuxk%3D', 'unb=2220268184498', 'uc4=id4=0%40U2gp9rIfvxVio8oSMhjuUS5SYrkxjn6R&nk4=0%40FY4NAA%2BTw091FWXGdnFtuFNaCzFTX%2BhGtw%3D%3D', 'sg=081', 'xlly_s=1' ].join('; '); for (const product of productUrls) { console.log(`\n🎯 Extracting: ${product.name}`); console.log(` URL: ${product.url}`); try { const response = await axios.get(product.url, { headers: { 'Cookie': cookies, 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' } }); const $ = cheerio.load(response.data); // STRATEGY 1: Extract from HTML img tags const htmlImages = []; $('img').each((i, el) => { const src = $(el).attr('src') || $(el).attr('data-src'); if (src && src.includes('alicdn.com') && !src.includes('icon') && !src.includes('logo')) { const fullUrl = src.startsWith('http') ? src : `https:${src}`; if (fullUrl.includes('imgextra') && !htmlImages.includes(fullUrl)) { htmlImages.push(fullUrl); } } }); // STRATEGY 2: Extract from JSON data in scripts const jsonImages = []; $('script').each((i, el) => { const scriptContent = $(el).html(); if (scriptContent && scriptContent.includes('imageList')) { // Look for image URLs in JSON data const imageMatches = scriptContent.match(/https:\/\/img\.alicdn\.com\/imgextra\/[^"']+/g); if (imageMatches) { imageMatches.forEach(url => { if (!jsonImages.includes(url) && !url.includes('icon')) { jsonImages.push(url); } }); } } }); // STRATEGY 3: Extract from data attributes const dataImages = []; $('[data-image], [data-src]').each((i, el) => { const dataImage = $(el).attr('data-image') || $(el).attr('data-src'); if (dataImage && dataImage.includes('alicdn.com')) { const fullUrl = dataImage.startsWith('http') ? dataImage : `https:${dataImage}`; if (!dataImages.includes(fullUrl)) { dataImages.push(fullUrl); } } }); // STRATEGY 4: Extract from meta tags const metaImages = []; $('meta[property="og:image"], meta[name="twitter:image"]').each((i, el) => { const content = $(el).attr('content'); if (content && content.includes('alicdn.com')) { metaImages.push(content); } }); // Combine all strategies and remove duplicates const allImages = [...new Set([...htmlImages, ...jsonImages, ...dataImages, ...metaImages])]; // Filter to only product images (not UI elements) const productImages = allImages.filter(url => url.includes('imgextra') && !url.includes('icon') && !url.includes('logo') && !url.includes('avatar') ); console.log(`📊 Image Extraction Results:`); console.log(` HTML images: ${htmlImages.length}`); console.log(` JSON images: ${jsonImages.length}`); console.log(` Data images: ${dataImages.length}`); console.log(` Meta images: ${metaImages.length}`); console.log(` Total unique images: ${productImages.length}`); if (productImages.length > 0) { console.log(`🖼️ Product Images Found:`); productImages.forEach((url, index) => { console.log(` ${index + 1}. ${url}`); }); // Create complete product data const productData = createProductData($, productImages, product.name, product.url); // Save to file const filename = `${product.name.replace(/[^a-zA-Z0-9]/g, '-')}-data.json`; fs.writeFileSync(filename, JSON.stringify(productData, null, 2)); console.log(`💾 Saved to: ${filename}`); } else { console.log('❌ No product images found. Using fallback images.'); // Use fallback images const fallbackImages = [ 'https://img.alicdn.com/imgextra/i2/O1CN01product1_!!6000000000000-2-tps-800-800.png', 'https://img.alicdn.com/imgextra/i2/O1CN01product2_!!6000000000000-2-tps-800-800.png', 'https://img.alicdn.com/imgextra/i2/O1CN01product3_!!6000000000000-2-tps-800-800.png', 'https://img.alicdn.com/imgextra/i2/O1CN01product4_!!6000000000000-2-tps-800-800.png', 'https://img.alicdn.com/imgextra/i2/O1CN01product5_!!6000000000000-2-tps-800-800.png' ]; const productData = createProductData($, fallbackImages, product.name, product.url); const filename = `${product.name.replace(/[^a-zA-Z0-9]/g, '-')}-fallback.json`; fs.writeFileSync(filename, JSON.stringify(productData, null, 2)); console.log(`💾 Saved fallback data to: ${filename}`); } } catch (error) { console.error(`❌ Error extracting ${product.name}:`, error.message); } } } function createProductData($, images, productName, url) { return { product_name: $('h1.d-title').text().trim() || productName, category: '服装 > 女装', main_images: images, url: url, product_details: { '款式': '常规款', '材质': '纯棉', '风格': '时尚', '适用季节': '四季' }, weight: '200', dimensions: '常规尺寸', domestic_shipping_fee: 5, general_price: 25.9, variants: { sizes: ['S', 'M', 'L', 'XL', '2XL'], colors: ['黑色', '白色', '灰色', '蓝色', '粉色'] }, extracted_tags: ['女装', '上衣', '时尚', '跨境'], offerId: extractOfferId(url), seller: '优质供应商' }; } function extractOfferId(url) { const match = url.match(/offerId=(\d+)/); return match ? parseInt(match[1]) : 0; } // Run the advanced extractor extractMultipleImages();