File size: 6,894 Bytes
9913f6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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();