sudheer-ranga / aliexpress-product-scraper

Get Aliexpress product details as a json response including feedbacks, variants, shipping info, description, images, etc.,
MIT License
231 stars 88 forks source link

TypeError: Cannot read property 'descriptionUrl' of undefined #35

Closed alkhachatryan closed 8 months ago

alkhachatryan commented 1 year ago

Hello. Sometimes this lib throws an exception:TypeError: Cannot read property 'descriptionUrl' of undefined

OS: Ubuntu 22.04 Node Version: v14.17.6 Lib version: ^1.0.6

alkhachatryan commented 1 year ago

@sudheer-ranga hi :)

rafaelsg-01 commented 10 months ago

Change the aliexpress-product-scraper/src/aliexpressProductScraper.js file code to this one:

const puppeteer = require('puppeteer');
const cheerio = require('cheerio');

const Variants = require('./variants');
const Feedback = require('./feedback');

async function AliexpressProductScraper(productId, feedbackLimit) {
  const FEEDBACK_LIMIT = feedbackLimit || 10;
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  /** Scrape the aliexpress product page for details */
  await page.goto(`https://www.aliexpress.com/item/${productId}.html`);
  const aliExpressData = await page.evaluate(() => runParams);

  const data = aliExpressData.data;

  /** Scrape the description page for the product using the description url */
  const descriptionUrl = data.productDescComponent.descriptionUrl;
  await page.goto(descriptionUrl);
  const descriptionPageHtml = await page.content();

  /** Build the AST for the description page html content using cheerio */
  const $ = cheerio.load(descriptionPageHtml);
  const descriptionData = $('body').html();

  /** Fetch the adminAccountId required to fetch the feedbacks */
  const adminAccountId = await page.evaluate(() => adminAccountId);
  await browser.close();

  let feedbackData = [];

  if (data.feedbackComponent.totalValidNum > 0) {
    feedbackData = await Feedback.get(
      data.productInfoComponent.id,
      adminAccountId,
      data.feedbackComponent.totalValidNum,
      FEEDBACK_LIMIT
    );
  }

  /** Build the JSON response with aliexpress product details */
  const json = {
    title: data.productInfoComponent.subject,
    categoryId: data.productInfoComponent.categoryId,
    productId: data.productInfoComponent.id,
    totalAvailableQuantity: data.inventoryComponent.totalAvailQuantity,
    description: descriptionData,
    orders: data.tradeComponent.formatTradeCount,
    storeInfo: {
      name: data.sellerComponent.storeName,
      companyId: data.sellerComponent.companyId,
      storeNumber: data.sellerComponent.storeNum,
      followers: data.sellerComponent.followingNumber,
      ratingCount: data.sellerComponent.positiveNum,
      rating: data.sellerComponent.positiveRate
    },
    ratings: {
      totalStar: 5,
      averageStar: data.feedbackComponent.averageStar,
      totalStartCount: data.feedbackComponent.totalValidNum,
      fiveStarCount: data.feedbackComponent.fiveStarNum,
      fourStarCount: data.feedbackComponent.fourStarNum,
      threeStarCount: data.feedbackComponent.threeStarNum,
      twoStarCount: data.feedbackComponent.twoStarNum,
      oneStarCount: data.feedbackComponent.oneStarNum
    },
    images:
      (data.imageModule &&
        data.imageModule.imagePathList) ||
      [],
    feedback: feedbackData,
    variants: Variants.get(data.priceComponent.skuPriceList),
    specs: data.productPropComponent.props,
    currency: data.currencyComponent.currencyCode,
    originalPrice: {
      min: data.priceComponent.origPrice.minAmount.value,
      max: data.priceComponent.origPrice.maxAmount.value
    },
    salePrice: {
      min: data.priceComponent.discountPrice.minActivityAmount 
        ? data.priceComponent.discountPrice.minActivityAmount.value 
        : data.priceComponent.discountPrice.minAmount.value,
      max: data.priceComponent.discountPrice.maxActivityAmount 
        ? data.priceComponent.discountPrice.maxActivityAmount.value 
        : data.priceComponent.discountPrice.maxAmount.value,
    }
  };

  return json;
}

module.exports = AliexpressProductScraper;

This code updates the file to the new Aliexpress object names. 🙂

sudheer-ranga commented 9 months ago

@alkhachatryan I have released a new version v2.0.2. There were breaking changes due to aliexpress changing their website layout and variables. Please update to the latest version.

sudheer-ranga commented 8 months ago

Closing this issue for now. Feel free to reopen or create new issue if the latest update is not working.