Actor

fvitas/amazon-search-offers

  • Builds
  • latest 0.0.18 / 2018-06-23
  • Created 2018-06-12
  • Last modified 2018-06-23
  • grade 2

Description

Sadly, this actor has no description.


API

To run the actor, send a HTTP POST request to:

https://api.apify.com/v2/acts/fvitas~amazon-search-offers/runs?token=<YOUR_API_TOKEN>

The POST payload will be passed as input for the actor. For more information, read the docs.


Example input

Content type: application/json; charset=utf-8

{
    "product": "iphone",
    "count": 20
}

Source code

Based on the apify/actor-node-chrome:beta Docker image (see docs).

const Apify = require('apify')

const amazonUrl = 'https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords='
const offerUrl = 'https://www.amazon.com/gp/offer-listing/'

async function collectProductIds(page) {
    return await page.evaluate(() => {
        return Array.from(document.querySelectorAll('[data-asin]:not(.AdHolder)'))
                    .map(a => a.dataset['asin'])
    })
}

async function collectOffers(page) {
    return await page.evaluate(() => {
        return Array.from(document.querySelectorAll('.olpOffer'))
                    .map(offer => ({
                        sellerName: offer.querySelector('.olpSellerName').textContent.trim(),
                        price:      offer.querySelector('.olpOfferPrice').textContent.trim()
                    }))
    })
}

Apify.main(async () => {
    const input = await Apify.getValue('INPUT')
    
    if (!input || !input.product) throw new Error('no product')
    if (!input || !input.count) throw new Error('no count')

    const browser = await Apify.launchPuppeteer({ headless: true })
    const page = await browser.newPage()

    const store = await Apify.openDataset()

    await page.goto(amazonUrl + input.product, { waitUntil: 'networkidle2', timeout: 3000000 })

    let ids = await collectProductIds(page)

    for (let id of ids) {
        await page.goto(offerUrl + id, { waitUntil: 'networkidle2', timeout: 3000000 })

        let offers = await collectOffers(page)

        while (true) {
            if (offers.length >= input.count || !await page.$('.a-last:not(.a-disabled)')) break

            await page.click('.a-last:not(.a-disabled)')
            await page.waitForSelector('.olpOffer')

            let moreOffers = await collectOffers(page)
            offers.push(...moreOffers)
        }

        await store.pushData({ id, offers })
    }

    await browser.close()
})