1import { Actor } from 'apify';
2
3import cheerio from 'cheerio';
4import getSellerInformation from './sellerInfo.js';
5import { createPuppeteerRouter } from 'crawlee';
6
7await Actor.init();
8
9const input = await Actor.getInput();
10const { ASIN } = input;
11
12export const router = createPuppeteerRouter();
13
14router.addDefaultHandler(async ({ page, request }) => {
15 const content = await page.content();
16
17 let $ = cheerio.load(content);
18
19 const asinNumbers = [];
20
21 $('div[data-asin][data-component-type="s-search-result"]').each((index, element) => {
22 asinNumbers.push($(element).attr('data-asin'));
23 });
24
25 console.log(asinNumbers);
26
27 const specificASIN = ASIN;
28 const placement = asinNumbers.indexOf(specificASIN) + 1;
29 console.log(`Product ASIN ${specificASIN} is placed at position: ${placement} on the page`);
30
31
32
33 if(ASIN){
34
35 await page.goto(`https://www.amazon.com/dp/${ASIN}`)
36 await page.waitForSelector('#sellerProfileTriggerId');
37
38 await page.click('#sellerProfileTriggerId');
39 await page.waitForSelector('#effective-timeperiod-rating-year-description');
40
41 const sellerPageContent = await page.content();
42 $ = cheerio.load(sellerPageContent);
43
44 const sellerRating = $('#effective-timeperiod-rating-year-description').text().trim();
45 const sellerName = $('#page-section-detail-seller-info > div > div > div > div:nth-child(2) > span:nth-child(2)').text().trim();
46 const sellerAddress = $('#page-section-detail-seller-info > div > div > div > div:nth-child(4)').text().trim();
47
48 console.log("Sellers Rating: ", sellerRating);
49 console.log("Seller Name: ", sellerName);
50 console.log("Seller Address: ", sellerAddress);
51
52 const dataset = await Actor.openDataset('SKYBOX');
53 await dataset.pushData({ASIN, placement, sellerRating, sellerName, sellerAddress});
54 }
55
56});
57
58router.addHandler('detail', async ({ page, request }) => {
59
60});