Actor picture

Amazon Test Actor

maksym-klnn/amazon-test-actor

No credit card required

Author's avatar
  • Modified
  • Users6
  • Runs24

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

This file is 130 lines long. Only the first 50 are shown. Show all

const Apify = require('apify');

Apify.main(async () => {
    const keyword = (await Apify.getValue('INPUT')).keyword || 'ipad'
    const amazonBaseUrl = 'https://www.amazon.com'
    const firstPageUrl = 'https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=' + keyword
    const offerListBaseUrl = 'https://www.amazon.com/gp/offer-listing/'

    const requestQueue = await Apify.openRequestQueue();

    await requestQueue.addRequest(new Apify.Request({
        url: firstPageUrl,
        userData: {
            label: "START"
        }
    }));

    const dataset = await Apify.openDataset(keyword)
    const {itemCount} = await dataset.getInfo()
    if (itemCount) await dataset.delete()


    const crawler = new Apify.PuppeteerCrawler({
        requestQueue,
        launchPuppeteerOptions: {headless: true},
        handlePageFunction: async ({page, request}) => {
            if (request.userData.label === "START") {
                const searchResult = await page.$('#s-results-list-atf.s-result-list');
                let res = await searchResult.$$eval('li.s-result-item', elems => {
                    return elems.map(el => {
                        const asin = el.getAttribute('data-asin')
                        const link = el.querySelector('.s-access-detail-page');
                        let itemUrl = link.getAttribute('href');
                        const title = link.querySelector('h2').getAttribute('data-attribute');
                        return {
                            asin,
                            itemUrl,
                            title
                        }
                    })
                })
                res = res.map(item => {
                    const urlArr = item.itemUrl.split(/&url=/);
                    if (urlArr && urlArr.length === 2) {
                        item.itemUrl = decodeURIComponent(urlArr[1]);
                    }
                    item.keyword = keyword
                    return item;
                })